Read a line of text

Read a line of text

Postby Jeff Barnes » Wed Jan 26, 2011 2:25 pm

Hi,

Is there a function to read a specific line of text from a .txt file?

Something like: cText := ReadText( cFile, nLine )
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Read a line of text

Postby Jeff Barnes » Wed Jan 26, 2011 2:34 pm

Just an addition to my original post ....

I am looking for something where I do not have to load the entire file into memory if possible.
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Read a line of text

Postby nageswaragunupudi » Wed Jan 26, 2011 2:39 pm

Code: Select all  Expand view
function ReadText( cFile, nLine )

   local cBuf     := MemoRead( cFile )
   local nAt
   
   if ( nAt := NumAt( CRLF, cBuf, nLine ) ) == 0
      cBuf        := ''
   else
      cBuf        := SubStr( cBuf, nAt + 2 )
      if ( nAt := At( CRLF, cBuf ) ) > 0
         cBuf     := Left( cBuf, nAt - 1 )
      endif
   endif

return cBuf
 


Please link ct.lib
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Read a line of text

Postby Richard Chidiak » Wed Jan 26, 2011 2:54 pm

Jeff

This is what i use and it works ok for me
Code: Select all  Expand view


local   aEol     := { Chr(13) + Chr(10), Chr(10) }, ;
      SLINE    := SPACE(20), ;
      hFile

hFile := FOPEN(xxxxx)
WHILE HB_FReadLine( hFile, @sLine, aEol ) == 0
  // retreive sline here
END
fclose(hFile)

 


Hth

Richard
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Re: Read a line of text

Postby nageswaragunupudi » Wed Jan 26, 2011 3:15 pm

Jeff Barnes wrote:Just an addition to my original post ....

I am looking for something where I do not have to load the entire file into memory if possible.

In such a case, this is the way.
Code: Select all  Expand view
function ReadText( cFile, nLine )

   local hFile    := HB_FUse( cFile )
   local cLine    := ""
   
   HB_FGoto( nLine )
   if .not. HB_FEof()
      cLine       := HB_FReadLN()
   endif
   HB_FUse()
   
return cLine
 

These functions are available in xHarbour
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Read a line of text

Postby Jeff Barnes » Wed Jan 26, 2011 3:42 pm

Thanks everyone :D
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Read a line of text

Postby lailton.webmaster » Fri Jan 28, 2011 4:13 pm

I use this way:

Code: Select all  Expand view
cString := MemoRead( "File.ext" )

nLines  := MLCount( cString, 400) // Line Count

? MemoLine( cString,400, 2 ) // return line 2 of cstring
 


Work fine for me.
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: Read a line of text

Postby Gale FORd » Fri Jan 28, 2011 11:36 pm

Don't forget about ttxtfile() class. It works pretty good for manipulating text files.
Gale FORd
 
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 89 guests