Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

When an application needs to access a file, it must first open it using fs_fopen():

The return value of this function should always be verified as non-NULL before the application proceeds to access the file. The first argument of this function is the path of the file; if working directories are disabled, this must be the absolute file path, beginning with either a volume name or a ‘\’ (see µC/FS File and Directory Names and Paths). The second argument of this function is a string indicating the mode of the file; this must be one of the strings shown in . Note that in all instances, the ‘b’ (binary) option has no affect on the behavior of file accesses.

After a file is opened, any of the file access functions valid for that its mode can be called. The most commonly used functions are fs_fread() and fs_fwrite(), which read or write a certain number of ‘items’ from a file:

The return value, the number of items read (or written), should be less than or equal to the third argument. If the operation is a read, this value may be less than the third argument for one of two reasons. First, the file could have encountered the end-of-file (EOF), which means that there is no more data in the file. Second, the device could have been removed, or some other error could have prevented the operation. To diagnose the cause, the fs_feof() function should be used. This function returns a non-zero value if the file has encountered the EOF.

Once the file access is complete, the file must be closed; if an application fails to close files, then the file system suite resources such as file objects may be depleted.

An example of reading a file is given below:

  • No labels