Recent

Author Topic: Save text into a txt file  (Read 30671 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
Save text into a txt file
« on: October 28, 2010, 12:51:01 pm »
hi
how do i save the text of a memo into the end of a textfile?
thanks

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Save text into a txt file
« Reply #1 on: October 28, 2010, 01:07:19 pm »
Quote
how do i save the text of a memo into the end of a textfile?
Code: [Select]
var
  f: TextFile;
begin
  AppendFile(f,'That file you want to append');
  Rewrite(f);
  WriteLn('A single line');
  ... // more Write(Ln)(s)
  CloseFile(f);
end;

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Save text into a txt file
« Reply #2 on: October 28, 2010, 02:14:18 pm »
Or you could use streams:

Code: Pascal  [Select][+][-]
  1. var fs: TFileStream;
  2. begin
  3.   fs := TFileStream.Create('d:\temp\tekstfile.txt', fmOpenWrite);
  4.   fs.Seek(0,fsFromEnd);
  5.   Memo1.Lines.SaveToStream(fs);
  6.   fs.Free;
  7. end;
  8.  
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: Save text into a txt file
« Reply #3 on: October 28, 2010, 04:26:21 pm »
or use a temporary stringlist:

Code: Pascal  [Select][+][-]
  1.   with TStringList.Create do try
  2.     LoadFromFile(OpenDialog1.FileName);
  3.     AddStrings(Memo1.Lines);
  4.     SaveToFile(OpenDialog1.FileName);
  5.   finally
  6.     Free;
  7.   end;
  8.  

captian jaster

  • Guest
Re: Save text into a txt file
« Reply #4 on: October 28, 2010, 04:54:12 pm »
Really everything posted above me.
Code: Pascal  [Select][+][-]
  1. Program WriteToFile;//A Safe way
  2.  
  3. Var
  4.  T:TextFile;
  5. begin
  6.    AssignFile('file.txt',T);
  7.    {$I-}
  8.    try
  9.    Rewrite(T);
  10.    Writeln(T,'Hello World!');
  11.    finally
  12.    CloseFile(T);
  13.    {$I+}
  14.    end;
  15.    writeln('IO: '+String(IOResult));
  16.    Readln;
  17. end;
  18.  
A Tutorial written by some laz people:
http://wiki.lazarus.freepascal.org/File_Handling_In_Pascal

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Save text into a txt file
« Reply #5 on: October 29, 2010, 06:32:17 am »
Code: Pascal  [Select][+][-]
  1. //...
  2.    Rewrite(T);
  3. //...
This destroys the original contents of the file.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Save text into a txt file
« Reply #6 on: October 29, 2010, 08:20:11 am »
Quote
This destroys the original contents of the file.
God, it's been a long time since I use Append(File). The correct one should be:
Code: [Select]
AssignFile(f,'file name');
Append(f);
... // write it out
CloseFile(f);

captian jaster

  • Guest
Re: Save text into a txt file
« Reply #7 on: October 29, 2010, 04:02:55 pm »
Code: Pascal  [Select][+][-]
  1. //...
  2.    Rewrite(T);
  3. //...
This destroys the original contents of the file.
I know..
Rewrite is for New Files though...  I thought that was what OP was trying to do.  :-[

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Save text into a txt file
« Reply #8 on: December 12, 2010, 09:27:23 pm »
And how do i write a String variable into a textfile?
Thanks

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Save text into a txt file
« Reply #9 on: December 12, 2010, 09:30:14 pm »
Code: [Select]
var
  strum: TFileStream;
  tmp: string;
begin
  tmp := 'test test test.';
  strum := TFileStream.Create('file.txt', fmCreate);
  try
    strum.Seek(0, soBeginning);
    strum.Write(tmp[1], Length(tmp));
  finally
    strum.Free;
  end;
end; 

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Save text into a txt file
« Reply #10 on: December 12, 2010, 09:49:45 pm »
Hi,
I tried to make a smal program to make some code for me but it didn't work.The program should have made the code and saved it into a textfile. Does anybody know whats wrong with it?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Save text into a txt file
« Reply #11 on: December 12, 2010, 10:02:01 pm »
Try to use the above code instead.
« Last Edit: December 12, 2010, 10:04:30 pm by typo »

Sora-Kun

  • Full Member
  • ***
  • Posts: 162
  • I can smell your presence ...
    • Sora-Kun
Re: Save text into a txt file
« Reply #12 on: December 12, 2010, 11:23:07 pm »
Just use simple file access,
for example

Code: [Select]
var my_file: text;
begin
assignfile(my_file, '/home/myfile.txt');
reset(my_file); // let's open the file. If the file does not exist this will create an IOerror use the  {$I-} and {$I+} with the IOresult.
seek(my_file,filesize(f)); //set the pointer to last line of the file
// and the next is for writing the memo lines at the end of the file
for i := 0 to Memo1.Lines.count-1 do
   writeln(my_file, memo1.Lines[i]);
closefile(my_file);
end;

Hope this help you it's a very simple solution  :D
Enjoy coding
LinkOS
« Last Edit: December 12, 2010, 11:33:06 pm by Linkos »
if nothing suites you, make it your self!
The Revolution, Genesis. The next generation IDE.
If you want to help, PM me.

Made in Lazarus.
Soon, in The WWW.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Save text into a txt file
« Reply #13 on: December 12, 2010, 11:40:00 pm »
Whenever possible, prefer TFileStream. It is a cleaner solution.

For components, you can LoadFromFile them.
« Last Edit: December 12, 2010, 11:53:59 pm by typo »

captian jaster

  • Guest
Re: Save text into a txt file
« Reply #14 on: December 13, 2010, 07:53:51 pm »
Whenever possible, prefer TFileStream. It is a cleaner solution.

For components, you can LoadFromFile them.
For text files TStringList is much more simpler then TFileStream.
TFileStream is more advanced in a way.

 

TinyPortal © 2005-2018