SophiaFramework UNIVERSE 5.3 |
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
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. |
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 ... }
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. |
The method to divide data into several segments and read each segment separately | |
---|---|
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 ... }
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. |
If the Flush function is not called ... | |
---|---|
Data will not be written if the file is closed without calling the Flush function. |
The method to divide data into several segments and write them separately | |
---|---|
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |