f_open

The f_open function creates a file object to be used to access the file.

FRESULT f_open (
  FIL* FileObject,       /* Pointer to the blank file object structure */
  const TCHAR* FileName, /* Pointer to the file neme */
  BYTE ModeFlags         /* Mode flags */
);

Parameters

FileObject
Pointer to the file object structure to be created.
FileName
Pointer to a null-terminated string that specifies the file name to create or open.
ModeFlags
Specifies the type of access and open method for the file. It is specified by a combination of following flags.
ValueDescription
FA_READSpecifies read access to the object. Data can be read from the file. Combine with FA_WRITE for read-write access.
FA_WRITESpecifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.
FA_OPEN_EXISTINGOpens the file. The function fails if the file is not existing. (Default)
FA_OPEN_ALWAYSOpens the file if it is existing. If not, a new file is created.
To append data to the file, use f_lseek function after file open in this method.
FA_CREATE_NEWCreates a new file. The function fails with FR_EXIST if the file is existing.
FA_CREATE_ALWAYSCreates a new file. If the file is existing, it is truncated and overwritten.

Return Values

FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_NOT_READY, FR_NO_FILE, FR_NO_PATH, FR_INVALID_NAME, FR_DENIED, FR_EXIST, FR_WRITE_PROTECTED, FR_INVALID_DRIVE, FR_NOT_ENABLED, FR_NO_FILESYSTEM, FR_TIMEOUT, FR_LOCKED, FR_NOT_ENOUGH_CORE, FR_TOO_MANY_OPEN_FILES

Description

After f_open function succeeded, the file object is valid. The file object is used for subsequent read/write functions to identify the file. When close an open file object, use f_close function. If the modified file is not closed, the file data can be collapsed.

If duplicated file open is needed, read here carefully. However duplicated open of a file with write mode flags is always prohibited.

Before using any file function, a work area (file system object) must be registered to the logical drive with f_mount function. All API functions except for f_fdisk function can work after this procedure.

QuickInfo

Always available. The mode flags, FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW and FA_OPEN_ALWAYS, are not available when _FS_READONLY == 1.

Example (File Copy)

void main (void)
{
    FATFS fs[2];         /* Work area (file system object) for logical drives */
    FIL fsrc, fdst;      /* file objects */
    BYTE buffer[4096];   /* file copy buffer */
    FRESULT res;         /* FatFs function common result code */
    UINT br, bw;         /* File read/write count */


    /* Register work area for each volume (Always succeeds regardless of disk status) */
    f_mount(0, &fs[0]);
    f_mount(1, &fs[1]);

    /* Open source file on the drive 1 */
    res = f_open(&fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);
    if (res) die(res);

    /* Create destination file on the drive 0 */
    res = f_open(&fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
    if (res) die(res);

    /* Copy source to destination */
    for (;;) {
        res = f_read(&fsrc, buffer, sizeof buffer, &br);     /* Read a chunk of src file */
        if (res || br == 0) break; /* error or eof */
        res = f_write(&fdst, buffer, br, &bw);               /* Write it to the dst file */
        if (res || bw < br) break; /* error or disk full */
    }

    /* Close open files */
    f_close(&fsrc);
    f_close(&fdst);

    /* Unregister work area prior to discard it */
    f_mount(0, NULL);
    f_mount(1, NULL);
}

See Also

f_read, f_write, f_close, FIL, FATFS

Return