FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Read a line of text
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Read a line of text
Posted: Wed Jan 26, 2011 02: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)
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Read a line of text
Posted: Wed Jan 26, 2011 02: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)
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Read a line of text
Posted: Wed Jan 26, 2011 02:39 PM
Code (fw): Select all Collapse
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
Posts: 946
Joined: Thu Oct 06, 2005 07:05 PM
Re: Read a line of text
Posted: Wed Jan 26, 2011 02:54 PM
Jeff

This is what i use and it works ok for me
Code (fw): Select all Collapse
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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Read a line of text
Posted: Wed Jan 26, 2011 03: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 (fw): Select all Collapse
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
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Read a line of text
Posted: Wed Jan 26, 2011 03:42 PM

Thanks everyone :D

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 603
Joined: Sun May 04, 2008 08:44 PM
Re: Read a line of text
Posted: Fri Jan 28, 2011 04:13 PM
I use this way:

Code (fw): Select all Collapse
cString := MemoRead( "File.ext" )

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

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


Work fine for me.
Posts: 663
Joined: Mon Dec 05, 2005 11:22 PM
Re: Read a line of text
Posted: Fri Jan 28, 2011 11:36 PM

Don't forget about ttxtfile() class. It works pretty good for manipulating text files.

Continue the discussion