Board index » delphi » text files, rewriting

text files, rewriting

hi,
is it possible to erase a line from a text file and the put something in its
place?  The reason I'm asking is because I'm creating a bank machine
simulation in Pascal and everytime a user comes back, their balance will
probably change....so how can I change that in the text file?
For example in the text file, it will prolly appear...
Name
PIN number
balance
Name
PIN number
balance
.
.
.
Jen
 

Re:text files, rewriting


You can't physical delete something in a file, all you can do is to create a
temporary file, write everything, except the "bits" you want to delete, to
it, and erase the old file and then rename the temp. file.

Quote
"jenbw2" <jennifer-...@home.com> wrote in message

news:ZveO6.112688$CY.7032152@news1.rdc2.on.home.com...
| hi,
| is it possible to erase a line from a text file and the put something in
its
| place?  The reason I'm asking is because I'm creating a bank machine
| simulation in Pascal and everytime a user comes back, their balance will
| probably change....so how can I change that in the text file?
| For example in the text file, it will prolly appear...
| Name
| PIN number
| balance
| Name
| PIN number
| balance
| .
| .
| .
| Jen

Re:text files, rewriting


On Mon, 21 May 2001 20:03:05 GMT, "jenbw2" <jennifer-...@home.com>
wrote:

Quote
>hi,
>is it possible to erase a line from a text file and the put something in its
>place?  The reason I'm asking is because I'm creating a bank machine
>simulation in Pascal and everytime a user comes back, their balance will
>probably change....so how can I change that in the text file?
>For example in the text file, it will prolly appear...
>Name
>PIN number
>balance
>Name
>PIN number
>balance
>.
>.
>.
>Jen

Program BankSimulation;
    {A textfile cannot be modified in place without difficulty. One
plodding way is to copy the file over each time, but I'm sure you do
not want that. A preferred way is to make a file of records which can
be randomly accessed and updated.

I suggest you make your initial balance entry for each customer as
you show above.

The first part of your program can convert this textfile to a file of
records, each of which contains these 3 fields.

To do the transactions, you can make another textfile (or make
keyboard entries, if you wish) entering only the PIN and the amount of
the transaction, minus for withdrawals and plus for deposits.

Locate that PIN location in your file of records, read out the
whole record, modify the balance field, and write back the whole
record.

I'm sure you wish to do this yourself but here is a VERY simple
example of only 3 records of 1 field to help you get started. }

CONST source = 'dummyxyz.dat';

TYPE
myRec = Record
           balance:Real;
         End;
myFile = file of myRec;

VAR
single:myRec;
f:myfile;
ct:Integer;

Procedure ShowAll(VAR recF:myFile);
VAR sing:myRec;
Begin
     Reset(recF);
     While not eof(recF) do
     Begin
          Read(f, sing);
          Writeln(sing.balance:1:2);
     End;
     Writeln;
End;

Begin  {main}
     Assign(f, source);
     Rewrite(f);  {will be written in sequence}

     single.balance := 1234.56;
     Write(f, single);
     single.balance := 987.65;
     Write(f, single);
     single.balance := 555.55;
     Write(f, single);

     Writeln('ORIGINAL ENTRIES');
     ShowAll(f);

     {File numbers begin with 0. Displaying the records in reverse: }
     Writeln('IN REVERSE ORDER');
     For ct := 2 downto 0 Do
     Begin
          Seek(f, ct);
          Read(f, single);
          Writeln(single.balance:1:2);
     End;

     Seek(f, 1);  {modify 2nd record}
     Read(f, single);
     single.balance := single.balance + 150;
     Seek(f, 1);  {to write to same place}
     Write(f, single);

     Writeln; Writeln('NEW BALANCE, SEQUENTIAL ORDER');
     ShowAll(f);
readln;
end.

Re:text files, rewriting


I assume that Name, PIN number, balance is a record data type.
Then declare a file of this type of record.  Then it is a simple matter
to find a particular record within a file of records on disk, and write
to that file an alteration of the data that occurs

For example,
    type BankRecord = record
             Name: string;
             PIN number: Integer;
             Balance: Integer;
    end;

    type BankRecordFile = file of BankRecord;

You would of course open a var file not as a textfile but as a binary file.
Then you treat the file as a database of records, the number of which is
known by you "somehow" (possibly a header of some sort).  Anyway,
whenever an update to a particular record is required, you seek the data
record within the file, make the changes to the record in the program,
and then rewrite the data to the file,

Quote
"jenbw2" <jennifer-...@home.com> wrote in message

news:ZveO6.112688$CY.7032152@news1.rdc2.on.home.com...
Quote
> hi,
> is it possible to erase a line from a text file and the put something in
its
> place?  The reason I'm asking is because I'm creating a bank machine
> simulation in Pascal and everytime a user comes back, their balance will
> probably change....so how can I change that in the text file?
> For example in the text file, it will prolly appear...
> Name
> PIN number
> balance
> Name
> PIN number
> balance
> .
> .
> .
> Jen

Re:text files, rewriting


Text files are limited as databases so here is code that has records, an
array of records, a file of records, and that seeks in the file:

{$N+}

Type Nam1 = Record
 tuf: string[15];
 ruf: double;
 End;

Var
 F1: file of Nam1;
 R: array[0..10] of Nam1;

Procedure Rite;
Begin
WriteLn(R[0].tuf, R[0].ruf);
WriteLn(R[1].tuf, R[1].ruf);
WriteLn;
End;

Begin
R[0].tuf:= 'NameStringOne';
R[0].ruf:= 1234;
R[1].tuf:= 'NameStringTwo';
R[1].ruf:= 4321;

Assign(F1, 'Yile');
Rewrite(F1);
Write(F1, R[0], R[1]);

R[1].ruf:= 5678;

Seek(F1, 1);
Write(F1, R[1]);

Seek(F1, 0);
Read(F1, R[0], R[1]);

Rite;

Close(F1);
ReadLn;
End.

Other Threads