Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-04

read text file in lazarus

14 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dians

hi all, i have been watch a for a long time the growth up of lazarus , i'm interesting but i'm still newbie.. how do i read text file in lazarus , i have been looking for documentation but i have not still get it yet, i have ever read like this :

lbIndexOpen := False;
while not lbIndexOpen do
  begin
    try
      if FileExists(FileName) then
        fStream := TFileStream.Create(FileName, fmOpenReadWrite)
      else
        fStream := TFileStream.Create(FileName, fmCreate);
      lbIndexOpen := True;
    except
      on e: Exception do
        begin
          WriteLn(E.Message);
          Sleep(500);
        end;
      end;
  end;

does that methode how to read text file ..?

thank for respone..
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20090403/1987ec64/attachment.html

Reply by email

Christian Iversen
to Dians
Dians wrote 27 lines
hi all, i have been watch a for a long time the growth up of lazarus , i'm interesting but i'm still newbie.. how do i read text file in lazarus , i have been looking for documentation but i have not still get it yet, i have ever read like this : lbIndexOpen := False; while not lbIndexOpen do begin try if FileExists(FileName) then fStream := TFileStream.Create(FileName, fmOpenReadWrite) else fStream := TFileStream.Create(FileName, fmCreate); lbIndexOpen := True; except on e: Exception do begin WriteLn(E.Message); Sleep(500); end; end; end; does that methode how to read text file ..?

This is not so good. In fact, it's a little bit insane ;-)

You are creating a TFileStream in every iteration of the loop, none of which are later freed. Also, there's a sleep(), which seems quite strange. Try this instead:

S := TStringList.Create();
S.LoadFromFile(Filename);
for X := 0 to S.Count-1 do
   writeln('Line ', X, ': ', S[X]);
S.Free();
Med venlig hilsen Christian Iversen

Reply by email

Graeme Geldenhuys
to Dians: hi all, i have been watch a for a long time the growth up of lazarus , i'm interesting…

2009/4/3 Dians :

interesting but i'm still newbie.. how do i read text file in lazarus ,? i have been looking for documentation but i have not still get it yet, i have

You could also use the RTL as follows:

var
  sl: TStringList;
begin
  sl := TStringList.Create;
  sl.LoadFromFile('/tmp/readme.txt');
  // now you can access each line of the text file via
  //   sl.Strings[i]
  // see help on TStringList and TStrings for more info
  sl.Free;
end;

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Dians
to Graeme Geldenhuys

is there any documentation or link, which i be able to read to use all feature of lazarus/FPC like Lazarus/FPC from scracth ,from instalation (in linux) until to use of feature of lazarus/FPC.

2009/4/3 Graeme Geldenhuys

Show quoted text 35 lines
2009/4/3 Dians : > interesting but i'm still newbie.. how do i read text file in lazarus , i > have been looking for documentation but i have not still get it yet, i have You could also use the RTL as follows: var sl: TStringList; begin sl := TStringList.Create; sl.LoadFromFile('/tmp/readme.txt'); // now you can access each line of the text file via // sl.Strings[i] // see help on TStringList and TStrings for more info sl.Free; end; Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20090404/263f59d1/attachment.html

Reply by email

Graeme Geldenhuys
to Dians

2009/4/3 Dians :

is there any documentation or link, which i be able to read to use all feature of lazarus/FPC like Lazarus/FPC from scracth ,from instalation (in linux) until to use of feature of lazarus/FPC.

Simply press F1 in Lazarus and it will probably find you some help. eg: http://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

Also if you go to the www.freepascal.org website and follow the Documentation link, you can download extensive PDF documentation covering many topics like fpdoc, fpmake, RTL, FCL etc.. Lots to read. :-)
Alternatively post Lazarus related questions here. Or post language questions in the FPC-Users mailing list.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

svaa
to Dians: is there any documentation or link, which i be able to read to use all feature of…

Hello:

I am astonished. It breaks my heart, I am crying: Nobody has even mentioned the old-fashion classic Pascal way for reading a text file. Traitors. ;-)

Loading a text file in a TStringList with LoadFromFile method is a good option, it is easy, it is simple and you can access to any line whenever you want. But, Why to load the whole file in memory if you only need to process the lines sequentially from the beginning to the end (particularly if it is a big file)?

Procedure ReadThisFile(FileName:string);
var
 f:TextFile;
 OneLine:string;
begin
 assignFile(f,FileName);
 reset(f);
 while not eof(f) do begin
    readln(f,OneLine);
    // do whatever you want with OneLine, for example write it on the output
    writeln(OneLine);
 closefile(f);
end;

PD: well, at least Borland Pascal way. I think standard Pascal uses "text", "assign" and "close" instead of TextFile", "AssignFile" and "Closefile"

Santiago A.

Reply by email

waldo kitty
to svaa
svaa wrote 5 lines
Hello: I am astonished. It breaks my heart, I am crying: Nobody has even mentioned the old-fashion classic Pascal way for reading a text file. Traitors. ;-)

my thoughts exactly... and folk wonder why today's programs are so bloated, slow, memory hogs and require a mini-cray just to operate with some decency of speed ;)

[TRIM]

PD: well, at least Borland Pascal way. I think standard Pascal uses "text", "assign" and "close" instead of TextFile", "AssignFile" and "Closefile"

long live Borland! lol ;)

NOTE: NEW EMAIL ADDRESS!! _\/ (@@) Waldo Kitty, Waldo's Place USA __ooO_( )_Ooo_____________________ telnet://bbs.wpusa.dynip.com _|_____|_____|_____|_____|_____|_____ http://www.wpusa.dynip.com ____|_____|_____|_____|_____|_____|____ ftp://ftp.wpusa.dynip.com _|_Eat_SPAM_to_email_me!_YUM!__|_____ wkitty42 -at- windstream.net

Reply by email

Graeme Geldenhuys
to waldo kitty
waldo kitty wrote
my thoughts exactly... and folk wonder why today's programs are so bloated, slow, memory hogs and require a mini-cray just to operate with some decency of speed ;)

Very true! Just curious, how do you know how much memory an application uses? Preferably a Linux and Windows method.

Does the 'heaptrc' unit do that?

eg: TStringList vs Old Fashioned TextFile
Both CLI test programs loaded the same sample text file and simply does a writeln() for each line of the sample text file and then quits. The sample text file is 26.4KB in size.

Old Fashioned TextFile
=================
$> ./project1
[...snip...]
Heap dump by heaptrc unit
522 memory blocks allocated : 163178/164720
522 memory blocks freed : 163178/164720
0 unfreed memory blocks : 0
True heap size : 131072
True free heap : 131072

TStringList
=========
$> ./project2
[...snip...]
Heap dump by heaptrc unit
457 memory blocks allocated : 106081/107664
457 memory blocks freed : 106081/107664
0 unfreed memory blocks : 0
True heap size : 360448
True free heap : 360448

As you can see, one allocates more memory blocks, but the other uses less heap size. So which method is better?

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

waldo kitty
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
On Sat, Apr 4, 2009 at 6:13 PM, waldo kitty wrote: > my thoughts exactly... and folk wonder why today's programs are so bloated, > slow, memory hogs and require a mini-cray just to operate with some decency of > speed ;) Very true! Just curious, how do you know how much memory an application uses? Preferably a Linux and Windows method.

i was looking at the simple fact that most of today's coders and processes read entire datafiles into memory... i know that some of those i work with are over 300Meg and there's no way i want to tie up that much memory... i won't even speak of the time it can take to load it all but that is dependent on the machine doing the work, too... not all of us have the latest and greatest core duo or quads or what have you... most of my machines are gimmes from upgrades done at the local hospital and factories when they toss out their old stuff for newer...

NOTE: NEW EMAIL ADDRESS!! _\/ (@@) Waldo Kitty, Waldo's Place USA __ooO_( )_Ooo_____________________ telnet://bbs.wpusa.dynip.com _|_____|_____|_____|_____|_____|_____ http://www.wpusa.dynip.com ____|_____|_____|_____|_____|_____|____ ftp://ftp.wpusa.dynip.com _|_Eat_SPAM_to_email_me!_YUM!__|_____ wkitty42 -at- windstream.net

Reply by email

Christian Iversen
to waldo kitty
waldo kitty wrote 16 lines
Graeme Geldenhuys wrote: > On Sat, Apr 4, 2009 at 6:13 PM, waldo kitty wrote: >> my thoughts exactly... and folk wonder why today's programs are so bloated, >> slow, memory hogs and require a mini-cray just to operate with some decency of >> speed ;) > Very true! Just curious, how do you know how much memory an > application uses? Preferably a Linux and Windows method. i was looking at the simple fact that most of today's coders and processes read entire datafiles into memory... i know that some of those i work with are over 300Meg and there's no way i want to tie up that much memory... i won't even speak of the time it can take to load it all but that is dependent on the machine doing the work, too... not all of us have the latest and greatest core duo or quads or what have you... most of my machines are gimmes from upgrades done at the local hospital and factories when they toss out their old stuff for newer...

Well you didn't tell us that. When the data files become that size, you really move from a data file approach to a database approach.

And for this, text files are just not that great. If you really have files in the size of 300mb+, you should look at something like sqlite. Depending on your use case, it's almost certainly going to help a lot and prevent you from re-inventing the wheel.

Med venlig hilsen Christian Iversen

Reply by email

svaa
to Graeme Geldenhuys: Very true! Just curious, how do you know how much memory an application uses? Preferably…

Hello:

I have made my two cents benchmark.

Two small programs (just FPC file, without Lazarus). one with LoadFromFile and one with readln(file,-..).

For small files, the LoadFromFile is faster. For big files, readln way is faster (in my system, for over 150K files, old-fashioned becomes faster). The bigger the file is, the better readln way is. I must admit that I was a little puzzled, I thought that readln way would always be faster because I thought: "They do the same, read a file, but the Tstringlist must do more work to handle the class and allocate memory."

But they don't do the same, LoadFromFile loads the file from disk in big blocks, the bigger the systems allows. On the other hand, "readln", reads only until it finds an EOL (well I suppose the operating system reads a minimum size of block and caches a little), so it send many I/o/ commands, one per line. In the old days I used to do a similar trick using blockread. I suppose that when the file is bigger, the overhead of allocating and deallocating memory is bigger.

So the conclusion: For many cases, loadFromFile is faster. Even when it's not faster it may be better because it makes many tasks easier.

My complain was that when Dians asked about how to read a text file, the right answer should had been showing both ways.

In fact, I pushed the benchmark to the limit with 1Gb file. "readln" program processed the file, but TStringList popped an out of memory. But that was not the big problem, the problem was that with TStringlist, for a minute, the system turned almost irresponsive, while, with "readln", I didn't notice anything.

Having things in memory is a good idea many times, particularly if you must read data several times you waste a lot of time reading from disk, but we also must be aware that memory is a valuable resource. When we use memory we are punishing the rest of processes running in our system. I know I am not showing a secret, but I am afraid we are forgetting it,... we don't balance pros and cons anymore, we just grab the memory.

Show quoted text 11 lines
Very true! Just curious, how do you know how much memory an application uses? Preferably a Linux and Windows method. Does the 'heaptrc' unit do that? eg: TStringList vs Old Fashioned TextFile Both CLI test programs loaded the same sample text file and simply does a writeln() for each line of the sample text file and then quits. The sample text file is 26.4KB in size.

Reply by email

Hans-Peter Diettrich
to svaa
Two small programs (just FPC file, without Lazarus). one with LoadFromFile and one with readln(file,-..).

You forgot the other streams (TFileStream...), which allow for (block) read. They only lack a ReadLn function (at least in Delphi), which has to be added somehow.

DoDi

Reply by email

Luca Olivetti
to svaa: Hello: I have made my two cents benchmark. Two small programs (just FPC file, without…

En/na svaa ha escrit:

My complain was that when Dians asked about how to read a text file, the right answer should had been showing both ways.

Note that both require exclusive access to the file. If you want shared access (e.g. to read a file that some other process has open for writing, like a log file) you have to use lower level functions or use streams (which, as Hans-Peter says, lack a ReadLn method). It's a pity that neither the standard file operations nor loadfromfile have a mean to specify shared access (or maybe they do but I couldn't find it).

Bye

Luca Olivetti Wetron Automatizaci?n S.A. http://www.wetron.es/ Tel. +34 93 5883004 (Ext.133) Fax +34 93 5883007

Reply by email