Link to home
Start Free TrialLog in
Avatar of trinas1
trinas1

asked on

Text File Editing Problem

I have created a text(.txt) file using the code below. I have to edit and
change the value of "Numberofbooks:= ?' .
How can I do this without altering (deleting) the rest of the file


Program Library;
Var
 ArchBiblio:Text;
 Code            :String[10];
 Namebook        :String[50];
 Numberofbooks   :String[3];

Begin
  Assign(ArchBiblio,'BIBLIOTEC.TXT');
  Rewrite(ArchBiblio);

     Code       :='0001';
     WriteLn(ArchBiblio,Code);
     Namebook     :='Chess Games';
     WriteLn(ArchBiblio,Namebook);
     Numberofbooks:='3';
     WriteLn(ArchBiblio,Numberofbooks);

     Writeln(ArchBiblio);

     Code       :='0002';
     WriteLn(ArchBiblio,Code);
     Namebook     :='Hello America';
     WriteLn(ArchBiblio,Namebook);
     Numberofbooks:='4';
     WriteLn(ArchBiblio,Numberofbooks);

 Close(ArchBiblio);

End.
Avatar of VIBESPRO
VIBESPRO
Flag of Antigua and Barbuda image

use your code above to make the file first then try this:  Let me know if it works right...


Program Library_2;
Var
 ArchBiblio:Text;
 Code            :String[10];
 Namebook        :String[50];
 Numberofbooks   :String[3];

Begin
  Assign(ArchBiblio,'BIBLIOTEC.TXT');
  Reset(ArchBiblio);

{ first get the data that was stored in the file }

     Read(ArchBiblio,Code);
     Read(ArchBiblio,Namebook);
     Read(ArchBiblio,Numberofbooks);
     Read(ArchBiblio);

 Close(ArchBiblio);
{ now close the file and reopen it to write data to it }

  Assign(ArchBiblio,'BIBLIOTEC.TXT');
  Rewrite(ArchBiblio);

     WriteLn(ArchBiblio,Code);
     WriteLn(ArchBiblio,Namebook);

{ Here is where you want to make the  
  change }
     Numberofbooks:='your choice';
     WriteLn(ArchBiblio,Numberofbooks);

     Writeln(ArchBiblio);

 Close(ArchBiblio);

I understand what you want to do.  You want to change one line the file without actually changing anything else!

That cannot be done without rewriting the file.  You must first store the data of the file into your variables as shown above then make the neccesary changes you wish and write it back to the file.

End.
ASKER CERTIFIED SOLUTION
Avatar of bakry99
bakry99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trinas1
trinas1

ASKER

Thank`s Bakry