What is the best way read a txt file?

Post Reply
ShumingWang
Posts: 454
Joined: Sun Oct 30, 2005 6:37 am
Location: Guangzhou(Canton),China

What is the best way read a txt file?

Post by ShumingWang »

hi,
I know xharbour has functions:

hfile:=hb_Fuse("ccc.txt")
while !hb_feof()
c1:=hb_freadln()
hb_fskip(1)
end
hb_fuse()
fclose(hfile)
but harbour without.

Are there lists of what functions each harbour libs files includes ?

Regards!
Shuming Wang
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post by James Bott »

How about just:

cText := memoread( "myfile.txt")

Regards,
James
demont frank
Posts: 167
Joined: Thu Mar 22, 2007 11:24 am

Post by demont frank »

James,

I think memoread has a limitation (or at least memoline) : linelength must be less as 254

Frank
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post by Antonio Linares »

Frank,

MemoRead() is fine but MemoLine() should not be used as it is very slow.

Please review fwh\samples\RE.prg to see how to read lines from text, real fast :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Post by Enrico Maria Giordano »

Antonio Linares wrote:Frank,

MemoRead() is fine but MemoLine() should not be used as it is very slow.

Please review fwh\samples\RE.prg to see how to read lines from text, real fast :-)
xHarbour offers HB_FReadLine() that should be fast enough.

EMG
Colin Wisbey
Posts: 56
Joined: Mon Jul 03, 2006 2:34 am

Post by Colin Wisbey »

Antonio,
RE.prg uses TTxtFile which, although I haven't used it, seems to assume each line is already terminated by CRLF. Wouldn't your suggestion only apply if the text file had no "soft" lines (which a normal paragraph would have)?

Col
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post by Antonio Linares »

Colin,

> RE.prg uses TTxtFile

Thats an old version of samples\RE.prg. The current RE.prg sample does not use TTxtFile and does it in a total different way:

Code: Select all

      cTxtFile = MemoRead( cRCFile )
      nFrom = 1

      while nFrom < Len( cTxtFile )
         cLine = ExtractLine( cTxtFile, @nFrom )
         ...
         SysRefresh()
      end
ExtractLine() is a new FWH function
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Post by nageswaragunupudi »

If we know we are dealing with a text file containing fixed width lines

Code: Select all

for nFrom := 1 to nSize step nLineWidth
   cline := substr( ctext, nfrom, nlinewidth )
next

to extract a particular line number :
cline := substr( ctext, ( nlinenumber- 1 ) * nlinewidth + 1, nlinewidth )
Extractline from FWH and the above code are enough to deal with text files containing variable length lines or fixed width lines.

If we like OOPS we can put the same code in a class.
Regards

G. N. Rao.
Hyderabad, India
Post Reply