• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.
0
trinas1
Asked:
trinas1
1 Solution
 
VIBESPROCommented:
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.
0
 
bakry99Commented:
Hi Trinas1
  You have to create a new file and then
read from old file ('BIBLIOTEC.TXT'),
Write to the new file created, and  when you read  'Numberofbooks' you change it if you like.
  Try This:
Program Library;
Var
 ArchBiblio, Temp: Text;
 Code            : String[10];
 Namebook        : String[50];
 Numberofbooks   : String[3];
Begin
  Assign(ArchBiblio,'BIBLIOTEC.TXT');  
  Assign(Temp, 'TempFile.Txt');
  Reset(ArchBiblio);
  ReWrite(Temp);
  While Not Eof(ArchBiblio) do
  Begin
     Readln(ArchBiblio, Code);
     Writeln(Temp, Code);
     Readln( ArchBiblio, NameBook);
     Writeln(Temp, NameBook);
     Readln(ArchBiblio, Numberofbooks);
     Writeln('The book is : ', BookName);
     Write('Howmany Books : ');
     Readln(Numberofbooks);
     Writeln(Temp, Numberofbooks);
     Readln(ArchBiblio );
     Writeln(Temp);
   end;{Of While}  
   Close(Temp);
   Close(ArchBiblio);
   Erase(ArchBiblio);
   Rename(Temp,'BIBLIOTEC.TXT');
end.

          Bakry
0
 
trinas1Author Commented:
Thank`s Bakry
 
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now