[fpc-pascal] Seek with text file

Rik van Kekem rik at graficalc.nl
Tue Apr 3 13:03:28 CEST 2018


Op 03-04-2018 12:03 schreef James Richters:
> 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);
> 
And what do you think FILESIZE(myfile) holds?
You would only strip the last character from the file with - 1.

Besides... file of String isn't a thing because String is a pointer to a 
area with characters and you can't have a file of string. You could do 
file of String[80] but in that case you don't have a normal text-file 
anymore.

How large are the files?
The easiest is to just load it into a TStringList and replace the last line.

If you have very very large files you would need to scan for the last 
line. You could do that from beginning to end but it would be better to 
do it from end to begin and stopping at the first CRLF. You could do 
that by opening it as stream of Text and stepping backwards in the file.

But a TStringList is really the easiest for small to medium text-files.

Rik


More information about the fpc-pascal mailing list