FileSys::Open( FileOpenMode, Error * )

Open the file name specified by the path protected FileSys member for reading or writing as specified by the argument FileOpenMode.

Virtual?

Yes

 

Class

FileSys

 

Arguments

FileOpenMode mode

Mode to open the file, either FOM_READ (open for read) or FOM_WRITE (open for write)

 

Error *error

returned error status

Returns

void

 

Notes

The default implementation of Open() is called every time there is a need to create or access a file on the client workspace.

Operating systems typically return a handle to the opened file, which is then used to allow future read/write calls to access the file.

Your implementation must correctly report any system errors that may occur during the open.

Example

To use open() to open a log file for writing:

FileSys *f = FileSys::Create( FST_ATEXT );
Error e;
StrBuf m;
m.Append( "example: text to append to a log file\r\n" );

f->Set( "C:\\logfile.txt" );
f->Open( FOM_WRITE, &e );
f->Write( m.Text(), m.Length(), &e );
f->Close( &e );

To reimplement Open() to report errors with Error::Sys(), provide debugging output, and use the FileSysDemo member “fd” to hold the file handle returned from the open() system call:

void FileSysDemo::Open( FileOpenMode mode, Error *e )
{
    this->mode = mode;

    int bits = ( mode == FOM_READ ) ? O_RDONLY
                            : O_WRONLY|O_CREAT|O_APPEND;

    if ( ( fd = open( Name(), bits, PERM_0666 ) ) < 0 )
    {
        e->Sys( mode == FOM_READ ? "open for read" : "open for write",
            Name() );
    }

    if ( DEBUG )
    {
        printf( "Debug (Open): '%s' opened for '%s'\n", Name(),
            mode == FOM_READ ? "read" : "write" );
    }
}