Recent

Author Topic: How to create a txt file from Lazarus?  (Read 1100 times)

Jvan

  • Full Member
  • ***
  • Posts: 181
How to create a txt file from Lazarus?
« on: September 29, 2020, 09:57:02 pm »
How to create one and to delete it after?

Thanks.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: How to create a txt file from Lazarus?
« Reply #1 on: September 29, 2020, 10:14:22 pm »
You can use FileCreate (then FileWrite):
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

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

Others useful reading materials:
https://wiki.freepascal.org/Files
http://www.pascal-programming.info/lesson8.php
https://wiki.freepascal.org/File_Handling_In_Pascal
« Last Edit: September 29, 2020, 10:33:39 pm by Handoko »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to create a txt file from Lazarus?
« Reply #2 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.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to create a txt file from Lazarus?
« Reply #3 on: September 29, 2020, 11:35:03 pm »
Old school:
AssignFile()
Rewrite()
CloseFile()

Bart

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: How to create a txt file from Lazarus?
« Reply #4 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.

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: How to create a txt file from Lazarus?
« Reply #5 on: September 30, 2020, 04:09:11 am »
Thanks!

 

TinyPortal © 2005-2018