PrevNextUpHome SophiaFramework UNIVERSE 5.3

18.2. SFXFile Class

The SFXFile class is used to perform the file operations such as creating a file, moving a file, deleting a file, reading or writing data from or into the file storage with or without the stream, retrieving file attribute information, making a temporary file etc.

Data can be read from / written into the file storage using the SFXFile class as follows:

How to use the SFXFile class

  1. Create the SFXFile instance(the SFXFile storage).
  2. Open the SFXFile storage with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function.
  3. Get the input / output stream with the SFXFile::GetStreamReader / SFXFile::GetStreamWriter function.
  4. Read / write data from / into the SFXFile storage through the input / output stream.
  5. * If the input / output stream is not used, data can be read from / written into the SFXFile storage with the SFXFile::Read / SFXFile::Write function.
  6. Close the SFXFile storage with the SFXFile::Close function.
[Tip] File open mode

There are two kinds of file open modes: the Read Only mode and the Read Write mode.

In case of the file input, open the file in the read only mode with the SFXFile::OpenReadOnly function.

In case of the file output, open the file in the Read Write mode with the SFXFile::OpenReadWrite function.

[Note] File pointer

The SFXFile class has the file pointer. The SFXFile::Read / SFXFile::Write function reads / writes data from / into the file storage through the file pointer, which will advance by the amount of the read / written data.

It is possible to move the file pointer with the SFXFile::Seek / SFXFile::SeekStart / SFXFile::SeekEnd.

The below is the code to read data from file.

Example 18.1. Reading data from file

SFXFile file;                     // file
SFXAnsiStringStreamReader reader; // stream for reading data from the file
SFXAnsiString string;             // variable to read into

// open the file in the read only mode
if ((error = file.OpenReadOnly(SFXPath("/dir/data.txt"))) == SFERR_NO_ERROR) {

    // get the stream for reading data from the file
    // * the stream buffer is variable since the size argument is not specified
    if ((error = file.GetStreamReader(&reader)) == SFERR_NO_ERROR) {

        // read data from the file into the stream buffer
        // *1. the stream buffer will be expanded automatically depending on the size of data to be read into
        // *2. the read operation will finish by calling the Fetch function once
        if ((error = reader.Fetch()) == SFERR_NO_ERROR) { 

            // read data from the stream buffer into the string variable
            if ((error = reader.ReadSFXAnsiString(&string)) == SFERR_NO_ERROR) { 

                // succeed to read from the file

                // display the content of the string variable on BREW Output Window
                TRACE("%s", string.GetCString());

            }
        } 
        // release the stream for reading from the file
        reader.Release();
    }
    // close the file
    file.Close();
}
if (error != SFERR_NO_ERROR) {

    // if an error occurs
    ...
}
[Note] Reading data using the variable buffer stream

Each stream has its own buffer(stream buffer).

Since the stream for reading data from the file is gotten by calling the SFXFile::GetStreamReader function without the size argument, the variable buffer will be used. Therefore, the stream buffer will be expanded to the same size with the input file.

The SFXStreamReader::Fetch function reads data from the file into the stream buffer.

The SFXAnsiStringStreamReader::ReadSFXAnsiString function reads data from the stream buffer into the specified variable.

[Note] The method to divide data into several segments and read each segment separately

See Reading Data from File with Fixed Buffer Stream.

The below is the code to write data into file.

Example 18.2. Write data into file

SFXFile file;                     // file
SFXAnsiStringStreamWriter writer; // stream for writing data into the file
SFXAnsiString string("abcdefg");  // variable to write

// open the file in the read-write mode
if ((error = file.OpenReadWrite(SFXPath("/dir/data.txt"))) == SFERR_NO_ERROR) {

// get the stream for writing data into the file
// * the stream buffer is variable since the size argument is not specified
    if ((error = file.GetStreamWriter(&writer)) == SFERR_NO_ERROR) {

        // write data from the string variable into the stream buffer
        // * the stream buffer will be expanded automatically depending on the size of data to be written
        if ((error = writer.WriteSFXAnsiString(string)) == SFERR_NO_ERROR) {

            // write data from the stream buffer into the file
            // * the write operation will finish by calling the Flush function once
            error = writer.Flush();
        } 
        // release the stream for writing into the file
        writer.Release();
    } 
    // close the file
    file.Close();
} 
if (error != SFERR_NO_ERROR) {

    // if an error occurs
    ...
}
[Note] Write data using the variable buffer stream

Each stream has its own buffer(stream buffer).

Since the stream for writing data into the file is gotten by calling the SFXFile::GetStreamWriter function without the size argument, the variable buffer will be used. Therefore, the stream buffer will be expanded to the same size with the output file.

The SFXAnsiStringStreamWriter::WriteSFXAnsiString function writes data from the string variable into the stream buffer.

The SFXStreamWriter::Flush function writes data from the stream buffer into the file.

[Caution] If the Flush function is not called ...

Data will not be written if the file is closed without calling the Flush function.

[Note] The method to divide data into several segments and write them separately

See Writing Data into File with Fixed Buffer Stream.