FileSys::Read( const char *, int, Error * )
Attempt to read len
bytes of data from the object
referenced by the file handle (returned by the Open()
method) to the buffer
pointed to by buf
. Upon successful completion, Read()
returns the number of
bytes actually read and placed in the buffer.
Virtual? |
Yes |
|
Class |
||
Arguments |
|
pointer to buffer into which to read data |
|
length of data to read |
|
|
returned error status |
|
Returns |
|
number of bytes actually read |
Notes
The default implementation of Read()
is called every time
there is a need to read data from the file referenced by the Open()
call.
Your implementation must correctly report any system errors that might occur during I/O.
Example
To use Read()
to read a
line from a log file:
char line[80];
m.Set( msg );
FileSys *f = FileSys::Create( FST_ATEXT );
Error e;
f->Set( "C:\\logfile.txt" );
f->Open( FOM_READ, &e );
f->Read( line, 80, &e );
f->Close( &e );
To reimplement Read()
to
report errors with Error::Sys()
, provide debugging
output, and use the FileSysDemo
member “fd” to hold the file
handle returned from the read()
system call:
int FileSysDemo::Read( char *buf, int len, Error *e )
{
int bytes;
if ( ( bytes = read( fd, buf, len ) ) < 0 )
e->Sys( "read", Name() );
if ( DEBUG )
{
printf( "debug (Read): %d bytes\n", bytes );
}
return( bytes );
}