Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

C/C++ API Documentation | 2025.1

# 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 might occur during the open.

## Example

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

Copy

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

    f-&gt;Set( "C:\\logfile.txt" );
    f-&gt;Open( FOM_WRITE, &amp;e );
    f-&gt;Write( m.Text(), m.Length(), &amp;e );
    f-&gt;Close( &amp;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:

Copy

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

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

        if ( ( fd = open( Name(), bits, PERM_0666 ) ) &lt; 0 )
        {
            e-&gt;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" );
        }
    }

Your search for returned results