Board index » delphi » Saving data

Saving data

Hi
    I have several arrays setup some as stings and others as integers and
what I want to be able to do but can't find a wa as yet is to save the data
to a file so that when I close the program and the open it up again the data
in the arrays is not lost but is retrived fro the file or where ever.
Please could someone wrtie us a bit of code or point us to somewhere on the
internet where there is an example of somethning like this or if it can't be
done with pascal tell me and stop me looking for a way.

Thanks in advance Rob Stevenson

 

Re:Saving data


Hey Rob,
I'm sorry but i don't have the answers for you :o(, but i'm gonna be chaaky and
say when u get the answers email them to me too.
I'm having the same problem.
Obviously when i find out i will send it on to you too!

regards

jo
Lavene

Re:Saving data


You need to something like these 2 procedures :

All the best,
  Tim

Var
  F:Text
  Data1:Array[1..2]of String;
  Data2:Array[1..2]of Integer;

Procedure SaveData;
Begin
  Assign(F,'test.dat');
  Rewrite(F);
  Write(F,Data1[1]);
  Write(F,Data1[2]);
  Write(F,Data2[1]);
  Write(F,Data2[2]);
  Close(F);
End;

Procedure LoadData;
Begin
  Assign(F,'test.dat');
  Reset(F);
  Read(F,Data1[1]);
  Read(F,Data1[2]);
  Read(F,Data2[1]);
  Read(F,Data2[2]);
  Close(F);
End;

Re:Saving data


The concept isn't really hard.  First, declare a variable as 'text'  (e.g. var
InFile : text;)  then, the very beginning of the program main should be assign
(Infile,'c:\dir\file.ext');
then, if you want to write  to the file, do this next rewrite (InFile);  if you
want to read from the file do this reset (InFile);  you can either read or write
and a time, not both.  now, to actually do the reading and writing, do this:  write
(Infile, 'blah blah blah', varname);  works the same with read.  When you're done
with the files, make sure you close (Infile);  to EVERY file.  If you don't, you're
asking for data loss.

Jay Knight
Rytsar...@iname.com
htpp://jhknight.nexus.olemiss.edu

Re:Saving data


UKHostTIM schrieb:
Quote

> You need to something like these 2 procedures :

> Var
>   F:Text
>   Data1:Array[1..2]of String;
>   Data2:Array[1..2]of Integer;

> Procedure SaveData;
> Begin
>   Assign(F,'test.dat');
>   Rewrite(F);
>   Write(F,Data1[1]);
    writeln(F,...
>   Write(F,Data1[2]);
>   Write(F,Data2[1]);
>   Write(F,Data2[2]);
>   Close(F);
> End;

> Procedure LoadData;
> Begin
>   Assign(F,'test.dat');
>   Reset(F);
>   Read(F,Data1[1]);
    readln(F, ....
>   Read(F,Data1[2]);
>   Read(F,Data2[1]);
>   Read(F,Data2[2]);
>   Close(F);
> End;

Re:Saving data


Quote
In article <19981123174658.12387.00002...@ng28.aol.com>, ukhost...@aol.comNOJUNK (UKHostTIM) wrote:
>You need to something like these 2 procedures :
>All the best,
>  Tim
>Procedure SaveData;
>Procedure LoadData;

Tim is right. You need something /like/ his procedures but use WRITELN and
READLN instead of WRITE and READ as when you READ a string from a file
then TP will read the entire line or up to 255 chars.

More efficient would be comma delimited files - depending upon the content of
your strings. A more flexible approach would be creating your own text tokens
to signal what the data on the rest of the line will be. Eg:

Read (InFile,TokenChar); (* READ FIRST LETTER OF LINE *)
CASE TokenChar OF
 'N': BEGIN (* READ A NUMBER *)
        READLN (InFile,NumberData[NumberIndex]);
        Inc(NumberIndex);
        END;
 'S': BEGIN (* READ A STRING *)
        READLN (InFile,StringData[StringIndex]);
        Inc(StringIndex);
        END;
 ELSE Writeln ('ReadData Token error line ', StringIndex+NumberIndex,
                      ' "',TokenChar,'"');
END; (* CASE *)

If your data is fixed and repeating then perhaps you should consider bunging
it all into a RECORD and creating a FILE of that record type.

Re:Saving data


In article <73e1gj$n8...@bunyip.cc.uq.edu.au>, Simon <porcup...@n2.com>
writes

Quote
>If your data is fixed and repeating then perhaps you should consider bunging
>it all into a RECORD and creating a FILE of that record type.

With the added advantage that it's compact and requires no delimiters
that waste storage. Also it's really hard to read unless you know the
format.

--
Oz

Re:Saving data


On Mon Nov 23, remove wrote:

Quote
>I have several arrays setup some as stings and others as
>integers and what I want to be able to do but can't find a wa
>as yet is to save the data to a file so that when I close the
>program and the open it up again the data in the arrays is not
>lost but is retrived fro the file or where ever. Please could
>someone wrtie us a bit of code or point us to somewhere on the
>internet where there is an example of somethning like this or
>if it can't be done with pascal tell me and stop me looking
>for a way.

Efficiently storing and fetching large amounts of data is
crucial to the success of many programs. Depending on the amount
of data involved, variations on the following technique should
be used.

Since the file on disk need not be people readable, you may as
well read and write the data as formatted in the computer.
Rather than use read/write, use blockread/blockwrite.

These two procedures work on files of type FILE!

var
   db     : file;

You can read/write any amount of data up to 64KB. You open/close
them the usual way (see the manual).

procedure
   dbload;
var
   readlen    : word;
begin
   assign(db, 'c:\...');
   reset(db);

   blockread(db, array1, sizeof(array1), readlen);
   blockread(db, array2, sizeof(array2), readlen);
   ...
   close(db);
end;

procedure
   dbsave;
var
   writelen    : word;
begin
   assign(db, 'c:\...');
   rewrite(db);

   blockwrite(db, array1, sizeof(array1), writelen);
   blockwrite(db, array2, sizeof(array2), writelen);
   ...
   close(db);
end;

Of course, you should check all return codes (reset, rewrite,
...) and check that the readlen equals the size of the array.

This is enough provided the things in the database are simple
arrays.

If you are using protected mode, you may have to copy the data
to a real mode buffer first.

BTW, one thing you should do is begin the database with a
version field, a word or perhaps a short string, that identifies
the version of the database. The first time it might contain
'MyDBV0.01'. Each time you change the DB format (it will
happen), you change this field value.

The dbsave routine writes the new DB identification and the new
data. The dbload routine reads the DB identification, and based
on what it finds, it either converts an earlier DB format to
the new format or reads the new format.

Where the data is stored in lists in RAM rather than arrays, you
can either read/write one list element at a time, or collect
many list entries into a buffer. The latter is much faster,
since IO operations are quite slow. Collecting 1000 20 byte list
entries into a 20KB buffer and then doing a single write can
speed up the database load/save by a factor of 100.

Brent
--
Brent Beach, Victoria, BC

Re:Saving data


On 24 Nov 1998 19:58:35 GMT, ae...@FreeNet.Carleton.CA (Brent Beach)
wrote:

Quote
>var
>   db     : file;

>You can read/write any amount of data up to 64KB. You open/close
>them the usual way (see the manual).

>procedure
>   dbload;
>var
>   readlen    : word;
>begin
>   assign(db, 'c:\...');
>   reset(db);
reset (db, 1);

>   blockread(db, array1, sizeof(array1), readlen);
>   blockread(db, array2, sizeof(array2), readlen);
>   ...
>   close(db);
>end;

>procedure
>   dbsave;
>var
>   writelen    : word;
>begin
>   assign(db, 'c:\...');
>   rewrite(db);

rewrite (db, 1);

Quote

>   blockwrite(db, array1, sizeof(array1), writelen);
>   blockwrite(db, array2, sizeof(array2), writelen);
>   ...
>   close(db);
>end;

>Brent
>--
>Brent Beach, Victoria, BC

Do not forget to specify the record size when you reset or rewrite the
file. If this parameter is omitted, BP assumes a record size of 128
bytes. Results of BlockRead and BlockWrite operations can become ...
interesting.  

regards,
Klaas

Re:Saving data


Rob Stevenson schrieb:

Quote
> I have several arrays setup some as stings and others as integers and
> what I want to be able to do but can't find a wa as yet is to save the data
> to a file so that when I close the program and the open it up again the data
> in the arrays is not lost but is retrived fro the file or where ever.

Use simply a textfile to store your data if the number of datafields is
fixed as in arrays. Here is a qiuck and dirty prg:

var
  StringArray : array[1..3] of String;
  IntegerArray : array[1..3] of Integer;
  I : Byte;
  F : Text;

  procedure ShowArray;
    {-Draw the array on the screen}
  var
    I : Byte;
  begin
    for I := 1 to 3 do
      Write(IntegerArray[I], ': ', StringArray[I], ', ');
    WriteLn;
  end;

begin
  {-Fill in some data}
  StringArray[1] := 'Save';
  StringArray[2] := 'into';
  StringArray[3] := 'files';
  for I := 1 to 3 do
    IntegerArray[I] := I;

  ShowArray;

  {-Save it to a file}
  Assign(F, 'TEST.DAT');
  Rewrite(F);
  for I := 1 to 3 do begin
    WriteLn(F, StringArray[I]);
    WriteLn(F, IntegerArray[I]);
  end;
  Close(F);

  {-Make the array empty}
  FillChar(StringArray, SizeOf(StringArray), 0);
  FillChar(IntegerArray, SizeOf(IntegerArray), 0);

  ShowArray;

  {-Read the data back from the file}
  Reset(F);
  for I := 1 to 3 do begin
    ReadLn(F, StringArray[I]);
    ReadLn(F, IntegerArray[I]);
  end;
  Close(F);

  ShowArray;
end.

By, Andreas.

Other Threads