xHarbour Reference Documentation > Class Reference (textmode) xHarbour Developers Network  

TStreamFileReader()

Creates a new TStreamFileReader object.

Syntax

TStreamFileReader():new( <cFileName>, ;
                        [<nOpenMode>] ) --> oTStreamFileReader

Arguments

<cFileName>
This is a character string holding the name of the file to open for reading. It must include path and file extension. If the path is omitted from <cFileName>, the file is searched in the current directory.
<nOpenMode>
A numeric value specifying the open mode and access rights for the file. #define constants from the FILEIO.CH file can be used for <nOpenMode> as listed in the table below:

File open modes
ConstantValueDescription
FO_READ0Open file for reading
FO_WRITE1Open file for writing
FO_READWRITE *)2Open file for reading and writing

Constants that define the access or file sharing rights can be added to an FO_* constant. They specify how file access is granted to other applications in a network environment.

File sharing modes
ConstantValueDescription
FO_COMPAT *)0Compatibility mode
FO_EXCLUSIVE16Exclusive use
FO_DENYWRITE32Prevent other applications from writing
FO_DENYREAD48Prevent other applications from reading
FO_DENYNONE64Allow others to read or write
FO_SHARED64Same as FO_DENYNONE

Return

The function returns a new TStreamFileReader object and method :new() initializes the object.

Description

TStreamFileReader objects encapsulate low-level file functions for reading a file as a stream of data. The class inherits from the abstract class TStream(), which maintains information about the stream size (file size) and current position within the stream data (file pointer).

Instance variables

:cFile
Name of the file being read.
:handle
Numeric low-level file handle

Methods

:close() --> self
Closes the data stream, or file.
:finalize() --> self
Destructor method.
:read( @<cBuffer>, [<nOffset>], <nBytes> ) --> nBytesRead
Reads data from the stream into memory.
:readByte() --> cByte
Reads a single byte from the data stream.
:seek( <nBytes>, <nOrigin> ) --> nPosition
Changes the position of the file pointer.

Info

See also:FileReader(), FileWriter(), TStream(), TStreamFileWriter()
Category: Object functions , xHarbour extensions
Header:fileio.ch
Source:rtl\stream.prg
LIB:lib\xhb.lib
DLL:dll\xhbdll.dll

Example

// The example reads an entire file in blocks of 4096 bytes and fills
// them into an array

   #define BLOCK_SIZE     4096

   PROCEDURE Main
      LOCAL oReader, cFile, cBuffer, nBytes, aBlocks

      aBlocks := {}
      cBuffer := SPace( BLOCK_SIZE )
      cFile   := "myfile.log"
      oReader := TStreamFileReader():new( cFile )

      IF oReader:handle < 0
         ? "Error opening file:", FError()
         QUIT
      ENDIF

      DO WHILE .T.
         nBytes := oReader:read( @cBuffer, , BLOCK_SIZE )

         IF nBytes == BLOCK_SIZE
            AAdd( aBlocks, cBuffer )
         ELSE
            // end of file reached
            AAdd( aBlocks, SubStr( cBuffer, nBytes ) )
            EXIT
         ENDIF
      ENDDO

      oReader:close()

      ? Len( aBlocks ), "Blocks of", BLOCK_SIZE, "bytes read"
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe