Lazarus

Programming => General => Topic started by: Jvan on September 29, 2020, 09:57:02 pm

Title: How to create a txt file from Lazarus?
Post by: Jvan on September 29, 2020, 09:57:02 pm
How to create one and to delete it after?

Thanks.
Title: Re: How to create a txt file from Lazarus?
Post by: Handoko on September 29, 2020, 10:14:22 pm
You can use FileCreate (then FileWrite):
https://www.freepascal.org/docs-html/rtl/sysutils/filecreate.html (https://www.freepascal.org/docs-html/rtl/sysutils/filecreate.html)

Or

In the link below go the the File handling section, there you can find Text file demo and Text File Demo #2 also improve performance in the General section:
https://wiki.freepascal.org/Portal:HowTo_Demos (https://wiki.freepascal.org/Portal:HowTo_Demos)

To delete a (text) file, you can use DeleteFile command:
https://www.freepascal.org/docs-html/rtl/sysutils/deletefile.html (https://www.freepascal.org/docs-html/rtl/sysutils/deletefile.html)

Others useful reading materials:
https://wiki.freepascal.org/Files (https://wiki.freepascal.org/Files)
http://www.pascal-programming.info/lesson8.php (http://www.pascal-programming.info/lesson8.php)
https://wiki.freepascal.org/File_Handling_In_Pascal (https://wiki.freepascal.org/File_Handling_In_Pascal)
Title: Re: How to create a txt file from Lazarus?
Post by: howardpc on September 29, 2020, 11:32:38 pm
You could also use the TStringList class which provides a SaveToFile() method.
You can use DeleteFile() to remove a file.
Title: Re: How to create a txt file from Lazarus?
Post by: Bart on September 29, 2020, 11:35:03 pm
Old school:
AssignFile()
Rewrite()
CloseFile()

Bart
Title: Re: How to create a txt file from Lazarus?
Post by: lainz on September 30, 2020, 01:24:54 am
I do this when debugging a program with text files...:

Code: Pascal  [Select][+][-]
  1. with TStringList.Create do
  2. begin
  3.   try
  4.     Add('Some variable:' + IntToStr(somenumber));
  5.     SaveToFile('file.txt');
  6.   finally
  7.     Free;
  8.   end;
  9. end;
  10.  

I delete the file myself after checking it... but you can delete it as here was explained by the other forum members.
Title: Re: How to create a txt file from Lazarus?
Post by: Jvan on September 30, 2020, 04:09:11 am
Thanks!
TinyPortal © 2005-2018