Discussion:
read text file in lazarus
Dians
2009-04-03 13:26:17 UTC
Permalink
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..
Christian Iversen
2009-04-03 13:57:10 UTC
Permalink
Post by 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
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
Graeme Geldenhuys
2009-04-03 13:58:33 UTC
Permalink
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/
Dians
2009-04-03 17:03:35 UTC
Permalink
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.
Post by Graeme Geldenhuys
Post by Dians
interesting but i'm still newbie.. how do i read text file in lazarus ,
i
Post by Dians
have been looking for documentation but i have not still get it yet, i
have
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
Graeme Geldenhuys
2009-04-03 23:33:17 UTC
Permalink
Post by 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/
svaa
2009-04-04 12:10:38 UTC
Permalink
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.
waldo kitty
2009-04-04 16:13:08 UTC
Permalink
Post by svaa
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]
Post by svaa
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
Graeme Geldenhuys
2009-04-04 21:12:43 UTC
Permalink
Post by waldo kitty
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/
waldo kitty
2009-04-05 01:56:40 UTC
Permalink
Post by Graeme Geldenhuys
Post by waldo kitty
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
Christian Iversen
2009-04-05 09:57:59 UTC
Permalink
Post by waldo kitty
Post by Graeme Geldenhuys
Post by waldo kitty
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
svaa
2009-04-06 00:16:25 UTC
Permalink
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.
Post by Graeme Geldenhuys
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.
Bernd Mueller
2009-04-06 08:23:46 UTC
Permalink
Post by svaa
So the conclusion: For many cases, loadFromFile is faster. Even when
increase the text buffer and try again ;-)

Regards, Bernd.
Hans-Peter Diettrich
2009-04-06 05:09:51 UTC
Permalink
Post by 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
Luca Olivetti
2009-04-15 07:59:30 UTC
Permalink
Post by svaa
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
Loading...