Recent

Author Topic: writting to text file question  (Read 587 times)

JLWest

  • Hero Member
  • *****
  • Posts: 550
writting to text file question
« on: April 01, 2019, 03:48:31 pm »
I'm writting to a text file with writeln(Outfile, Line); it writes the data fine but Is there a way to get it formatted or lined up in columns

{As it's written}
LELL    1    52.23919266    16.23113957    Bristol         
1NFL    5    41.520833333    2.105           
KMCI    8    41.52334596    2.10072339    Denver        sa
EB1N    14    52.23919266    16.23113957    Hoevenen        fde

{Would Like}

LELL     1      52.23919266    16.23113957    Bristol         
1NFL     5    41.520833333              2.105           
KMCI     8     41.52334596     2.10072339    Denver            sa
EB1N    14    52.23919266    16.23113957   Hoevenen        fde

JLWEST
Lazuras ver 2.0.2 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
3952 GB (1.5 SSD)

lucamar

  • Hero Member
  • *****
  • Posts: 1960
Re: writting to text file question
« Reply #1 on: April 01, 2019, 04:28:15 pm »
I'm writting to a text file with writeln(Outfile, Line); it writes the data fine but Is there a way to get it formatted or lined up in columns

Yes, two basic ways.
  • Easy way: use Format() to build the line;
  • more convoluted (and less flexible): use the old-style format options of write/writeln.
It should all be in the docs. :)
« Last Edit: April 01, 2019, 11:46:56 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 1775
Re: writting to text file question
« Reply #2 on: April 01, 2019, 11:19:44 pm »
You can use the old style as was stated by Lucamar and also there is the WriteStr which will create a
string formatted using the same features
 
At least that is good for the numbers I am not sure which way it works for Text if it does
anything at all, It may pad to the right instead.

But you also have PadLeft, PadRight and PadCenter functions in the StrUtils unit..

This all works nicely if you use a Mono space font...
 

lucamar

  • Hero Member
  • *****
  • Posts: 1960
Re: writting to text file question
« Reply #3 on: April 01, 2019, 11:39:37 pm »
Been looking around and it so happens that Write() and WriteLn() are not documented in the help files, so look in the wiki: Write (and WriteLn) and Formatting output.

But I really would use Format(), either as:
Code: Pascal  [Select]
  1. Line = Format(...);
or directly as:
Code: Pascal  [Select]
  1. WriteLn(Format(...));

ETA
[..] I am not sure which way it works for Text if it does
anything at all, It may pad to the right instead [...]

Yes, for strings (really, for any type) the result is always right-justified so I always use PadRight() for them and format only numeric values, if for some reason I can't (or won't) use Format()
« Last Edit: April 01, 2019, 11:52:08 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Thausand

  • Full Member
  • ***
  • Posts: 227
Re: writting to text file question
« Reply #4 on: April 02, 2019, 12:16:07 pm »
Been looking around and it so happens that Write() and WriteLn() are not documented in the help files, so look in the wiki: Write (and WriteLn) and
can see https://www.freepascal.org/docs-html/rtl/system/write.html and https://www.freepascal.org/docs-html/rtl/system/writeln.html

is format
Quote
The format of the numerical conversions can be influenced through the following modifiers: OutputVariable: NumChars [: Decimals ] This will print the value of OutputVariable with a minimum of NumChars characters, from which Decimals are reserved for the decimals. If the number cannot be represented with NumChars characters, NumChars will be increased, until the representation fits. If the representation requires less than NumChars characters then the output is filled up with spaces, to the left of the generated string, thus resulting in a right-aligned representation. If no formatting is specified, then the number is written using its natural length, with nothing in front of it if it's positive, and a minus sign if it's negative. Real numbers are, by default, written in scientific notation.

Not easy understand. try make test.

Can make same writestr https://www.freepascal.org/docs-html/rtl/system/writestr.html


lucamar

  • Hero Member
  • *****
  • Posts: 1960
Re: writting to text file question
« Reply #5 on: April 02, 2019, 02:51:47 pm »
Not easy understand. try make test.

It could be better explained but it's not so difficult either :)

The general sintax is: Value:MinimumWidth[:Decimals]
and the value will be right-justified in the field so, for example:
Code: Pascal  [Select]
  1.   MyString := 'Just a String';
  2.   MyFloat := 1.563;
  3.   Writeln('"', MyString:20, '"');
  4.   Writeln('"', MyFloat:20:4, '"');
will result in:
Code: [Select]
"       Just a String"
"              1.5630"
If the value to print is larger than the minimum width (for example, if MyString had been 30 characters long) then the minimum width is ignored and the value is printed in full.

That's basically all there is to it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 550
Re: writting to text file question
« Reply #6 on: April 02, 2019, 10:03:49 pm »
Got it. Thanks.
JLWEST
Lazuras ver 2.0.2 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
3952 GB (1.5 SSD)