Forum > General

dynamic array: load from and write to file

(1/1)

Blue1987:
I have a dynamic array of packed record and I would like to write it in a file, and to load it from the saved file


--- Code: ---Tsquadre = packed record
nome: shortstring;
stadio_nome: shortstring;
fonti_capitale_sociale: longint;
fonti_debiti: longint;
flussi_proventi_sponsor: longint;
cassa: longint;               {5000€}
budget: longint;              {5000€}
classifica_V: byte;           {0...38}
classifica_P: byte;           {0...38}
classifica_S: byte;           {0...38}
classifica_GF: byte;           {0...255}
classifica_GS: byte;          {0...255}
tattica: byte;                {1..20}
valutazione: byte;            {1+12*20}
titolare: array[1..18] of word; {0...n_calciatori}
end;
--- End code ---

in which way would you suggest that I proceed?

Blue1987:

--- Quote from: Blue1987 on September 10, 2011, 03:23:22 pm ---I have a dynamic array of packed record and I would like to write it in a file, and to load it from the saved file


--- Code: ---Tsquadre = packed record
nome: shortstring;
stadio_nome: shortstring;
fonti_capitale_sociale: longint;
fonti_debiti: longint;
flussi_proventi_sponsor: longint;
cassa: longint;               {5000€}
budget: longint;              {5000€}
classifica_V: byte;           {0...38}
classifica_P: byte;           {0...38}
classifica_S: byte;           {0...38}
classifica_GF: byte;           {0...255}
classifica_GS: byte;          {0...255}
tattica: byte;                {1..20}
valutazione: byte;            {1+12*20}
titolare: array[1..18] of word; {0...n_calciatori}
end;
--- End code ---

in which way would you suggest that I proceed?

--- End quote ---


I am solving this way...


--- Code: ---    assignfile(file_squadre, 'data_squadre_old.dat');
    reset(file_squadre);
    setlength(squadra_old,1);
    while not EoF(file_squadre) do
        begin
        SetLength(squadra_old, Length(squadra_old)+1); // Add a new team to the array
        read(file_squadre, squadra_old[high(squadra_old)]);
        end;
    closefile(file_squadre);
    n_squadre_old:=high(squadra_old);
--- End code ---

is there a better way to do the same...
... I mean, is it ok to  create an array of 1 record, and then adding thousands of record one by one???

are there problem with the allocation of memory? isn't it a bit slow? is there a faster way?

Laksen:
There's a much faster way. Simply store a longint before all records, indicating the total number of records following


--- Code: ---    assignfile(file_squadre, 'data_squadre_old.dat');
    reset(file_squadre);
    read(file_squadre, number_of_records);
    setlength(squadra_old,number_of_records);
    for i := 0 to number_of_records-1 do
        read(file_squadre, squadra_old[i]);
    closefile(file_squadre);
    n_squadre_old:=high(squadra_old);

--- End code ---

This could be made even faster by using a single BlockRead call instead of the reads in the for loop

Blue1987:

--- Quote from: Laksen on September 10, 2011, 05:29:48 pm ---There's a much faster way. Simply store a longint before all records, indicating the total number of records following


--- Code: ---    assignfile(file_squadre, 'data_squadre_old.dat');
    reset(file_squadre);
    read(file_squadre, number_of_records);
    setlength(squadra_old,number_of_records);
    for i := 0 to number_of_records-1 do
        read(file_squadre, squadra_old[i]);
    closefile(file_squadre);
    n_squadre_old:=high(squadra_old);

--- End code ---

This could be made even faster by using a single BlockRead call instead of the reads in the for loop

--- End quote ---

that's great! i simply did not know how to save more things in a single file! thanks!

Navigation

[0] Message Index

Go to full version