f_open


Use this function to open a file. The following modes are allowed for opening:

Mode Description
"r" Open existing file for reading. The stream is positioned at the beginning of the file.
"r+" Open existing file for reading and writing. The stream is positioned at the beginning of the file.
"w" Truncate file to zero length or create file for writing. The stream is positioned at the beginning of the file.
"w+" Open a file for reading and writing. The file is created if it does not exist; otherwise it is truncated. The stream is positioned at the beginning of the file.
"a" Open for appending (writing to end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
"a+" Open for reading and appending (writing to end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

Note the following:

  • The same file can be opened multiple times in “r” mode.
  • A file can only be opened once at a time in a mode which gives write access (that is, in “r+, “w”, “w+” , “a” or “a+” mode).

  • The same file can be opened multiple times in “r” mode and at the same time once in one of the “r+, “a” or “a+” modes which give write access.
  • If a file is opened in “w” or “w+” mode, a lock mechanism prevents it being opened in any other mode. This prevents opening of the file for reading and writing at the same time.

Note: There is no text mode. The system assumes that all files are in binary mode only.

Format

F_FILE * f_open (
    const char *   filename,
    const char *   mode )

Arguments

Argument Description Type
filename The file to be opened. const char *
mode The opening mode. const char *

Return values

Return value Description
F_FILE  * A pointer to the associated opened file handle.
0 The file could not be opened.

Example

void myfunc()
{
    F_FILE *file;
    char c;
    file = f_open( "myfile.bin", "r" );
    if (!file)
    {
        printf( "File cannot be opened!" );
        return;
    }
    f_read( &c, 1, 1, file );  /* Read one byte */
    printf( "'%c' is read from file", c );
    f_close( file );
}