Recent

Author Topic: Add string from Tmemo to text file  (Read 2110 times)

samoraj

  • New Member
  • *
  • Posts: 22
Add string from Tmemo to text file
« on: October 18, 2017, 07:06:58 am »
Hello, i have existing text file with information, and need to add more information in new line after existing from Tmemo. my code delete old information and i don't no how to change this
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.SendClick(Sender: TObject);
  3. begin
  4.  
  5.  
  6.      AssignFile(F, '\\SERVICEII-PC\print\sram\'+ ComboBox2.Text +'.txt');
  7.      Rewrite(F);
  8.      CloseFile(F);
  9.       TempStrings := TStringList.Create;
  10.       TempStrings.AddStrings(Memo2.Lines);
  11.        TempStrings.SaveToFile('\\SERVICEII-PC\print\sram\'+ComboBox2.Text+'.txt');
  12.        TempStrings.Free;
  13.  
  14. end;    
  15.  
  16.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14164
  • Probably until I exterminate Putin.
Re: Add string from Tmemo to text file
« Reply #1 on: October 18, 2017, 09:39:59 am »
You close the file.... If you want to append, use a TFilestream.
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add string from Tmemo to text file
« Reply #2 on: October 18, 2017, 10:06:58 am »
There are numerous ways to accomplish what you want. Here's one:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SendClick(Sender: TObject);
  2. var
  3.   sl: TStringList;
  4.   filename: String;
  5.  
  6.   function GetFileName: string;
  7.   const
  8.     TextFilePath = '\\SERVICEII-PC\print\sram\';
  9.   begin
  10.     Exit(TextFilePath + ComboBox2.Text + '.txt');
  11.   end;
  12.  
  13. begin
  14.   filename:=GetFilename;
  15.   if not FileExists(filename) then
  16.     Exit;
  17.   sl:=TStringList.Create;
  18.   try
  19.     sl.LoadFromFile(filename);
  20.     sl.AddStrings(Memo2.Lines);
  21.     sl.SaveToFile(filename);
  22.   finally
  23.     sl.Free;
  24.   end;
  25. end;

samoraj

  • New Member
  • *
  • Posts: 22
Re: Add string from Tmemo to text file
« Reply #3 on: October 18, 2017, 11:47:52 am »
Thanks  howardpc   ::)

 

TinyPortal © 2005-2018