Recent

Author Topic: Separate text in list format into text file!  (Read 1729 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Separate text in list format into text file!
« on: January 21, 2022, 08:00:34 pm »
I'm trying to separate text in line format in text file, but it does not work!  This is what I'm trying to accomplish.

Date:                Food:            Price:
01-03, 2022    Tomatoes     $1.05
10-03, 2022     Fries             $4.99
15-03, 2022     Bag               $0.50

This is what i looks now!
Date:                Food:            Price:
01-03, 2022    Tomatoes     $1.05
10-03, 2022     Fries         $4.99
15-03, 2022     Bag         $0.50

Is there commands in Pascal that correct this issue?

Thanks in advance.
Bob :(
Rob

paweld

  • Hero Member
  • *****
  • Posts: 966
Re: Separate text in list format into text file!
« Reply #1 on: January 21, 2022, 08:23:49 pm »
Format function: https://wiki.freepascal.org/Format_function
Code: Pascal  [Select][+][-]
  1.   memo1.Lines.Add(Format('%-12s   %-10s   %-8s', ['Date:', 'Food:', 'Price:']));
  2.   memo1.Lines.Add(Format('%-12s   %-10s   %8s', ['01-03, 2022', 'Tomatoes', '$1.05']));
  3.   memo1.Lines.Add(Format('%-12s   %-10s   %8s', ['10-03, 2022', 'Fries', '$4.99']));
  4.   memo1.Lines.Add(Format('%-12s   %-10s   %8s', ['15-03, 2022', 'Bag', '$0.50']));
  5.  
Best regards / Pozdrawiam
paweld

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Separate text in list format into text file!
« Reply #2 on: January 21, 2022, 10:07:43 pm »
Thanks paweld for the help.
I try to se how i can create a textfile with.
for a:=1 to 10 do begin
filewrite(myFile, format('%-12s', intToStr(myVar1[a])+ intToStr(myVar2[a])+intToStr(myVar3[a])  )  );
end;
But it not working! :(
Suggestions paweld?

Rob

paweld

  • Hero Member
  • *****
  • Posts: 966
Re: Separate text in list format into text file!
« Reply #3 on: January 21, 2022, 11:02:53 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   tf: TextFile;
  4.   i: Integer;
  5. begin
  6.   AssignFile(tf, 'test_file.txt');
  7.   if FileExists('test_file.txt') then
  8.     append(tf)
  9.   else
  10.     rewrite(tf);
  11.   for i := 1 to 100 do
  12.     writeln(tf, Format('%-13s %-5d   %5d', ['Interation no', i, Random(9999)]));
  13.   CloseFile(tf);
  14. end;  
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018