【转载】FATFS函数之——f_open & f_read

FATFS函数之——f_open & f_read

刚开始使用f_read和f_write时发现read/write老是出错,仔细查看源码发现,原来f_open文件时需要指定open 方式,这些个方式影响了后面的文件操作。

f_open函数声明如下:

 
  1. FRESULT f_open (

  2. FIL* fp, /* [OUT] Pointer to the file object structure */

  3. const TCHAR* path, /* [IN] File name */

  4. BYTE mode /* [IN] Mode flags */

  5. );

参数详细说明如下:

fp:Pointer to the blank file object structure to be created.
path: Pointer to a null-terminated string that specifies the file name to open or create.
mode: Mode flags that specifies the type of access and open method for the file. It is specified bya combination of following flags.

flags如下:

刚开始用的组合为FA_READ | FA_WRITE | FA_CREATE_ALWAYS,这样的话就读不出来数据,而换成FA_OPEN_ALWAYS就可以正常的读写了,且看FA_CREATE_ALWAYS最后一句:

it will be truncated and overwritten,意思是如果文件存在,则覆盖,所以会读不出数据。

 

f_read函数声明如下:

 
  1. FRESULT f_read (

  2. FIL* fp, /* [IN] File object */

  3. void* buff, /* [OUT] Buffer to store read data */

  4. UINT btr, /* [IN] Number of bytes to read */

  5. UINT* br /* [OUT] Number of bytes read */

  6. );

btr是用户要读的数据量,br是实际读取的数据量,这样当判断到br==0时,文件读完。

你可能感兴趣的:(STM32)