This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

fopen Modes and Zephyr FS_O_ flags

I am using the FAT FS on the nRF9160-DK to store data on an SD card.

The CLIB library provides Linix-like fopen, fclose, …

The flags passed to fopen are strings like “w”, “r”, “RW+”that control the behavior of the FS when opening a file.

 

There are options such as “the file must exist” and “create the file if it doesn’t exist.

What combination of FS_O_ flags corresponds to the various strings?

 

From www.cplusplus.com/.../

 

C string containing a file access mode. It can be:

"r"

read: Open file for input operations. The file must exist.

"w"

write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

"a"

append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.

"r+"

read/update: Open a file for update (both for input and output). The file must exist.

"w+"

write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

"a+"

append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").

The new C standard (C2011, which is not part of C++) adds a new standard subspecifier ("x"), that can be appended to any "w" specifier (to form "wx", "wbx", "w+x" or "w+bx"/"wb+x"). This subspecifier forces the function to fail if the file exists, instead of overwriting it.

  • Hi!

    FAT FS doesn't really have text / binary mode, as it just reads everything as bytes, so you can disregard the file-type specifier.

    Below is a table with the corresponding mode flags. As you can see from the asterisk and my comments below, some of the modes aren't supported directly in the fs_open function, so I have suggested workarounds to get the same functionality. 

    r FA_READ FS_O_READ
    w FA_CREATE_ALWAYS | FA_WRITE  FS_O_CREATE | FS_O_WRITE*
    a FA_OPEN_APPEND | FA_WRITE FS_O_APPEND | FS_O_WRITE
    r+ FA_READ | FA_WRITE FS_O_RDWR
    w+ FA_CREATE_ALWAYS | FA_READ | FA_WRITE  FS_O_CREATE | FS_O_RDWR*
    a+ FA_OPEN_APPEND | FA_WRITE | FA_READ FS_O_APPEND | FS_O_RDWR
    wx FA_CREATE_NEW | FA_WRITE FS_O_CREATE | FS_O_WRITE**
    w+x FA_CREATE_NEW | FA_READ | FA_WRITE FS_O_CREATE | FS_O_RDWR**

    *will not truncate already existing file, use fs_stat to check if file exists and if so, run fs_truncate() to truncate

    **will not fail if file exists, use fs_stat to check if file exists

    It's not clear to me why the Zephyr File System implementation doesn't include all the modes. I've asked about this on their Slack, so I'll let you know if I hear back. 

    Best regards,

    Heidi

  • Thanks so much.
    Do you think we should make this public?
    I am guessing others have the same question.

  • That's a good idea. I have requested this case be made public but it's ultimately up to the ticket owner, i.e you. 

Related