Page 1 of 1

What is the best way read a txt file?

Posted: Sat Jan 12, 2008 2:46 am
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

Posted: Sat Jan 12, 2008 5:53 am
by James Bott
How about just:

cText := memoread( "myfile.txt")

Regards,
James

Posted: Sat Jan 12, 2008 9:56 am
by demont frank
James,

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

Frank

Posted: Sat Jan 12, 2008 12:05 pm
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 :-)

Posted: Sat Jan 12, 2008 12:42 pm
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

Posted: Sun Jan 13, 2008 4:13 am
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

Posted: Sun Jan 13, 2008 4:21 am
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

Posted: Sun Jan 13, 2008 4:32 am
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.