Recent

Author Topic: Opening file with append  (Read 7671 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Opening file with append
« on: January 20, 2018, 09:10:43 pm »
How do I open a filehandle with append/create?
-Nugax

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Opening file with append
« Reply #1 on: January 20, 2018, 09:20:22 pm »
For TextFile: Append(), for other files, just open and seek end of file?

Bart

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Opening file with append
« Reply #2 on: January 20, 2018, 09:51:10 pm »
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Opening file with append
« Reply #3 on: January 20, 2018, 09:56:22 pm »
For TextFile: Append(), for other files, just open and seek end of file?

Bart


Does append write the data at the end of a TextFile type?
-Nugax

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Opening file with append
« Reply #4 on: January 20, 2018, 11:48:50 pm »
Yes, it does.

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Opening file with append
« Reply #5 on: January 21, 2018, 03:17:42 am »
Here I wrote a demo showing how to create new, append, and show data using text file:
http://forum.lazarus.freepascal.org/index.php/topic,37766.msg254800.html#msg254800

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Opening file with append
« Reply #6 on: January 21, 2018, 10:13:16 pm »
Keep in mind using "Append" requires you to test the IO status afterwards, in case the file does not exists.
InOutRes := 0;

AppendFile(F,'???????.???');
If InResult <> 0 Then Rewrite(F); //We'll assume the error is #2 "File Not found"

from here you can use Writeln to add lines to the file.

The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Opening file with append
« Reply #7 on: January 21, 2018, 10:47:41 pm »
The more logical approach would be:

Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3.  
  4. {SI+} //check for IO errors
  5. var
  6.   Fn: String;
  7.   F: TextFile;
  8. begin
  9.   Fn := 'path\to\file';
  10.   AssignFile(F, Fn);
  11.   if FileExists(Fn) then
  12.     Append(F)
  13.   else
  14.     Rewrite(F);
  15.   if IOResult <> 0 then
  16.   begin
  17.     writeln('Uanble to open file: ',Fn);
  18.     halt; //I opt to stop the program here, it is not mandatory
  19.   end;
  20.   //do stuff with the file. Check IOResult after each write yo do
  21.   ...
  22.   CloseFile(F)
  23. end;
  24.  

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Opening file with append
« Reply #8 on: January 22, 2018, 03:35:11 pm »
The more logical approach would be:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. {SI+} //check for IO errors
  4.   ...
  5.   if IOResult <> 0 then
  6.   ...
  7.  
I'm guessing that was implied {$IOCHECKS OFF}.

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Opening file with append
« Reply #9 on: January 22, 2018, 06:19:21 pm »
 :D :D :D :D :D :D :D
Specialize a type, not a var.

 

TinyPortal © 2005-2018