Recent

Author Topic: Append not working in one programme and working in another  (Read 423 times)

stephanos

  • Jr. Member
  • **
  • Posts: 81
Append not working in one programme and working in another
« on: January 03, 2026, 01:05:28 am »
Dear All

I am adding an append function in a large programme.  I have appended many times before but this time it is not working.  When compiled, I get an error message as follows:
Quote
“Error: SyntaxError “;” expected ”(” found”.
Below is the code

Code: Pascal  [Select][+][-]
  1. Var
  2.      fullpathfilename : Unicodestring;
  3.              testfile : TextFile;
  4. begin
  5. // lots of stuff
  6. // iteration starts
  7. // lots of stuff
  8.        AssignFile(testfile, 'GetPlayTimeTest.txt');  // the file
  9.        Append(testfile);
  10.        Writeln(testfile, fullpathfilename);
  11.        CloseFile(testfile);  // closes the file {* *}  
  12. // lots of stuff
  13. // iteration ends
  14. // lots of stuff
  15. end

The programme executable is in the same folder on the PC as 'GetPlayTimeTest.txt'.
Fullpathfilename changes its content on every iteration.  However, I need to know the content at each iteration.  So I decided to write/append the string to a text file, that I have already made.

However, Lazarus 4.0, 64 bit, Linux is producing the error when I compile it.
The error is with the opening ( of: Append(testfile);
It was expecting a semicolon, which is daft, but got an opening bracket, which is normal. 
In addition, when:
Code: Pascal  [Select][+][-]
  1. AssignFile(testfile, 'GetPlayTimeTest.txt');  // the file
was before the iteration I got the same error

Undeterred I made a new project of a simple form with one button, and on-click the following code executed.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   testfile : TextFile;
  4.   message : string;
  5. begin
  6. //Append each file and its path to a file
  7.     AssignFile(testfile, 'GetPlayTimeTest.txt');
  8.     Append(testfile);
  9.     //Writeln(testfile, 'a string literal');
  10.     message := 'a string';
  11.     Writeln(testfile, message);
  12.     CloseFile(testfile);  // closes the file
  13. end;

I tested a string literal, a string, and I changed the string to a unicode string.  The 4 lines of content of ‘GetPlayTimeTest.txt' are:
Quote
Append to this file
a string literal
a string
a 2nd string, that is unicode

The new project works.  There is no compiler error.  No conflict with a ; and (.  Moreover, I am using the same computer, the same version of Lazarus, and wearing the same socks, and I have rebooted as well.

I have read a lot, both online and in books.  My code is right.  What has occurred should not have.

Anyone with a solution will be honoured in tales of bravery and intelligence until the end of time.

Thanks and wait to hear

dseligo

  • Hero Member
  • *****
  • Posts: 1649
Re: Append not working in one programme and working in another
« Reply #1 on: January 03, 2026, 02:27:44 am »
Maybe you have some other procedure/function named 'Append'. Hold Ctrl and click on Append and see where that leads you.

Or try: System.Append(testfile);

Thaddy

  • Hero Member
  • *****
  • Posts: 18666
  • Jungle wars. And failing health it seems.
Re: Append not working in one programme and working in another
« Reply #2 on: January 03, 2026, 10:04:40 am »
Yes, posted crossed, thought the same;
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   testfile : Text;
  4.   mess : string;
  5. begin
  6. //Append each file and its path to a file
  7.     System.Assign(testfile, 'GetPlayTimeTest.txt');
  8.     System.Append(testfile);
  9.     //Writeln(testfile, 'a string literal');
  10.     mess := 'a string';
  11.     Writeln(testfile, mess);
  12.     System.Close(testfile);  // closes the file
  13. end;
Note: after append the file is write only. You need to re-open it to read.
« Last Edit: January 03, 2026, 10:07:23 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Nicole

  • Hero Member
  • *****
  • Posts: 1300
Re: Append not working in one programme and working in another
« Reply #3 on: January 03, 2026, 10:21:29 am »
This is a working way and I used it for a long time.
I quit.

I prefer to write such things:

Var SL: TStringList;

begin
SL:= TStringList.Create;
SL.LoadFile('myFile');
...
SL.Free;
end;

TStringList offers that many built-in functions I may need as sorting, reversing, checking for duplicates, saveToFile,...
that I personally prefer it in the meanwhle.

Thaddy

  • Hero Member
  • *****
  • Posts: 18666
  • Jungle wars. And failing health it seems.
Re: Append not working in one programme and working in another
« Reply #4 on: January 03, 2026, 12:34:47 pm »
@Nicole
The idea is to append and that can not be done with TStringlist.LoadFromFile, because that destroys any previous stream.

What you can do is use TMemoryStream.loadFromFile and then Append to the TStringlist with TStringlist.Append(TmemoryStream.Text);

But his old school approach should also work.
« Last Edit: January 03, 2026, 12:37:31 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12592
  • FPC developer.
Re: Append not working in one programme and working in another
« Reply #5 on: January 03, 2026, 12:41:14 pm »
But his old school approach should also work.

And can cope with larger files, and possibly less SSD wear.

Nicole

  • Hero Member
  • *****
  • Posts: 1300
Re: Append not working in one programme and working in another
« Reply #6 on: January 03, 2026, 02:15:23 pm »
Not sure, what about large files, my files are > 20 MB.
Sure, there are larger ones.

Not sure, if I got the task at all.
Case I did, I solve it by:
Code: Pascal  [Select][+][-]
  1. SL.LoadFromFile('myFile');
  2. SL.Insert(0, 'add me as first line');
  3. SL.SaveToFile('myFile');

stephanos

  • Jr. Member
  • **
  • Posts: 81
Re: Append not working in one programme and working in another
« Reply #7 on: January 04, 2026, 03:20:50 pm »
Dear All

Thanks for prompt responses

Yes, there was a process called Append.  System.Append() worked

Thanks to all

 

TinyPortal © 2005-2018