2

I am designing a program that writes to and stores data in a text file. The problem is that whenever I enter new data into the file, the old data is cleared and just that one entry (line) is there. I was originally saying Rewrite(tFile) every time but now use a boolean to check if the text file has been rewritten. I still get the same problem..

    begin
    AssignFile(tNotifications, 'Notifications.txt');
    if bRewritten = False then
      begin
        Rewrite(tNotifications);
        bRewritten := True;
      end;
     Writeln('test');
     CloseFile(tNotifications);
1
  • Better use TStringList. Commented Aug 12, 2018 at 11:54

3 Answers 3

14

Oldstyle solution (have you looked at See Also section in AssignFile help?):

AssignFile(tNotifications, 'Notifications.txt');
Append(tNotifications);
Writeln(tNotifications, newtext);

For fresh Delphi versions (help):

add IOUtils to uses
...
TFile.AppendAllText('Notifications.txt', newtext);  
Sign up to request clarification or add additional context in comments.

1 Comment

Note that AppendAllText does not include a line break, so if you want a line break, you'll have to add it yourself. Such as TFile.AppendAllText('Notifications.txt', newtext+sLineBreak);
6

After AssignFile() you must make a choise, whether to Reset to read the file, or if you are going to write to the file, whether to Append to the file or Rewrite the file.

If you want to retain existing data in the file and just append additional data, then you call Append().

If you want to replace existing data in the file with new data, then you call Rewrite.

I don't understand what the purpose of your boolean bRewritten is, I would expect you to set it somewhere else in your program as a signal for the file writing routine to either call Append() or ReWrite().


In an answer to an earlier question it was suggested to use a TStringList and its SaveToFile() method. If you would use TStringList and you would want to append, you would first read the file (StringList.ReadFromFile()), add new strings to the StringList and finally save (StringList.SaveToFile()). To rewrite the existing file you would just omit the reading before adding strings to the StringList and saving.

I support using a TStringList as a modern and more convenient solution.

4 Comments

A TStringList is very easy to use, but I wouldn't recommend it for a log file that gets updated very often. It is slow (because the entire file needs to be rewritten when appending just a single line) and it causes unnecessary writes on a ssd drive.
@Sebastian, so you would recommend old Pascal I/O instead?
Old pascal I/O doesn't support Unicode, so I avoid that. I'd go with TFile.WriteAllText and TFile.AppendAllText (but be careful because of Bug 13839).
Now I should think how to emulate TFile.WriteAllText in Delphi 6 in order to append UTF-8 strings in SSD friendly way.
0

Or examine "append". Btw: AssignFile can be substituted by the more correct Assign.

1 Comment

I'm not so sure about the correctness of Assign. See documentation: "Note: To avoid scope conflicts, AssignFile replaces the Assign procedure that was available in early versions of the Delphi product. However, for backward compatibility, Assign is still available."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.