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);
Kromster
  • 7,181
  • 7
  • 63
  • 111
joel_kruger
  • 115
  • 2
  • 3
  • 6

3 Answers3

13

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);  
MBo
  • 77,366
  • 5
  • 53
  • 86
  • 3
    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);` – Jerry Dodge Nov 30 '20 at 18:20
5

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.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • 1
    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 Z Aug 12 '18 at 20:56
  • @Sebastian, so you would recommend old Pascal I/O instead? – Victoria Aug 12 '18 at 21:56
  • 2
    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](https://quality.embarcadero.com/browse/RSP-13839)). – Sebastian Z Aug 14 '18 at 08:25
  • Now I should think how to emulate `TFile.WriteAllText` in Delphi 6 in order to append UTF-8 strings in SSD friendly way. – Paul Aug 17 '19 at 11:10
0

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

  • 3
    I'm not so sure about the correctness of Assign. [See documentation](http://docwiki.embarcadero.com/Libraries/XE7/en/System.AssignFile): "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." – Tom Brunberg Aug 12 '18 at 12:39