User`s guide

i.LON SmartServer 2.0 Programming Tools User’s Guide 233
File Access Methods
You can read and write to data files on the SmartServer using the following ANSI ‘C’ file methods:
fopen(), fread(), fwrite(), fseek()and, fclose().
fopen()
You can use the fopen()method to open a file and assign it a stream that can be identified by other
methods.
SYNTAX
FILE fopen (const char * filename, const char * mode);
The filename parameter specifies the name of the file to be opened. This parameter must
follow the file name specifications of the Eclipse SDK environment, and it may include a file path.
The mode parameter specifies the file access mode, which determines the operations that can be
performed on the file stream returned by this method. The file stream can either be a text or a
binary file. The mode can be one of the following values:
r
Opens an existing text file for reading.
w
Creates an empty text file for writing. If a file with the same name already exists, its
content is erased and the specified file is treated as a new empty file.
a
Appends data to the end of an existing text file. If the specified file does not exist, a new
file is created.
r+
Opens an existing text file for both reading and writing.
w+
Creates an empty text file for both reading and writing. If a file with the same name
already exists, its content is erased and the specified file is treated as a new empty file.
a+
Opens an existing text file for reading and appending data. All writing operations are
performed at the end of the file, protecting the previous content to be overwritten.
You can reposition the internal pointer to anywhere in the file for reading using the
fseek()method, but writing operations will move it back to the end of file.
If the specified file does not exist, a new file is created.
rb
Opens an existing binary file for reading.
wb
Creates an empty binary file for writing. If a file with the same name already exists, its
content is erased and the specified file is treated as a new empty file.
ab
Appends data to the end of an existing binary file. If the specified file does not exist, a
new file is created.
r+b
Opens a existing binary file for both reading and writing.
w+b
Creates an empty binary file for both reading and writing. If a file with the same name
already exists, its content is erased and the specified file is treated as a new empty file.
a+b
Opens an existing binary file for reading and appending data. All writing operations are
performed at the end of the file, protecting the previous content to be overwritten.
You can reposition the internal pointer to anywhere in the file for reading using the
fseek()method, but writing operations will move it back to the end of file.
If the specified file does not exist, a new file is created.
If the file has been opened successfully, this method returns a pointer to a FILE object that is used
to identify the stream in all further operations. Otherwise, a null pointer is returned.