I am trying to figure out how I can remove the last line of a text file to 
replace it with a new one.  I can do this with typed files with seek... but I 
am confused on how I could do this with just a text file.

If I use:

Var Myfile: Text;
       Mydata:String;

         Assign(myfile,'test.txt');
         append(myfile);
         Writeln(myfile,'New Line'+mydata);
         Close(myfile);
I can write to the end of the file, but I can't use seek to go to the end of 
the file minus one line.

If I try 
Var Myfile: file of Text;
       Mydata:String;

         Assign(myfile,'test.txt');
         Reset(myfile);
         Seek(myfile,FILESIZE(myfile)-1);
         Writeln(myfile,'New Line'+mydata);
         Close(myfile);
I cannot use writeln with typed files.... 

so I tried with write... 
Var Myfile: file of Text;
       Mydata:String;

         Assign(myfile,'test.txt');
         Reset(myfile);
         Seek(myfile,FILESIZE(myfile)-1);
         Write(myfile,'New Line'+mydata);
         Close(myfile);
I get Error: Incompatible types: got "ShortString" expected "Text"

So then I tried 
Var Myfile: file of String;
       Mydata:String;

         Assign(myfile,'test.txt');
         Reset(myfile);
         Seek(myfile,FILESIZE(myfile)-1);
         Write(myfile,'New Line'+mydata);
         Close(myfile);

Well that compiles, but when I run it the new line at the end of my file is 
what I wanted but followed by whole long line of garbage... some of it looks 
like the contents of memory that was not part of the string.  I'm guessing it 
attached a string of length 255 even though the string was not that long.

Can someone please help me understand this.  I really have no concept of the 
difference between 'text' and 'string'  The file I am trying to work with is 
just a regular text file like you would create with notepad..  Is there some 
way to open this file and use Seek to get to the line number I want?  It seems 
to me like the file is very well defined, a CRLF at the end of each line 
defines how long each line is, it seems like I would be able to seek X number 
of these records... but I can't figure out how to define it... or write just 
the part of the string with my actual data in it.

If I can't use seek then what do I do? Open it, reset it, then read the entire 
file except the last line then somehow then write my new line? 

Or do I have to read the whole file into a buffer or something, make my change, 
then write the whole thing out again?

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to