您的位置:首页 > 运维架构

fatfs 学习笔记--f_open用法(最新版本)

2017-07-15 09:53 295 查看


FatFs

DateRevision
May 21, 2017FatFs R0.13 (latest release)
Mar 04, 2017FatFs R0.12c
Sep 04, 2016FatFs R0.12b
Jul 10, 2016FatFs R0.12a
Apr 12, 2016FatFs R0.12
Sep 05, 2015FatFs R0.11a
Feb 09, 2015FatFs R0.11
Nov 09, 2014FatFs R0.10c
May 19, 2014FatFs R0.10b
Jan 15, 2014FatFs R0.10a
Oct 02, 2013FatFs R0.10
Jan 24, 2013FatFs R0.09b
Aug 27, 2012FatFs R0.09a
Sep 06, 2011FatFs R0.09
Jan 15, 2011FatFs R0.08b
Aug 16, 2010FatFs R0.08a
May 15, 2010FatFs R0.08
Nov 03, 2009FatFs R0.07e
Jun 21, 2009FatFs R0.07c
Apr 14, 2009FatFs R0.07a
Apr 01, 2008FatFs/Tiny-FatFs R0.06
Feb 03, 2008FatFs/Tiny-FatFs R0.05a
Aug 25, 2007FatFs/Tiny-FatFs R0.05
May 05, 2007FatFs/Tiny-FatFs R0.04b
Apr 01, 2007FatFs/Tiny-FatFs R0.04a
Feb 04, 2007FatFs/Tiny-FatFs R0.04
Dec 11, 2006FatFs/Tiny-FatFs R0.03a
Sep 22, 2006FatFs/Tiny-FatFs R0.03
Jun 10, 2006FatFs/Tiny-FatFs R0.02a
Jun 01, 2006FatFs/Tiny-FatFs R0.02
Apr 29, 2006FatFs/Tiny-FatFs R0.01


f_open

The f_open function opens a file.
FRESULT f_open (
FIL* fp,           /* [OUT] Pointer to the file object structure */
const TCHAR* path, /* [IN] File name */
BYTE mode          /* [IN] Mode flags */
);



Parameters

fpPointer to the blank file object structure.
pathPointer to the null-terminated string that specifies the file
name to open or create.
modeMode flags that specifies the type of access and open method for the file. It is specified by a combination of following flags.
FlagsMeaning
FA_READSpecifies read access to the object. Data can be read from the file.
FA_WRITESpecifies write access to the object. Data can be written to the file. Combine with FA_READfor read-write access.
FA_OPEN_EXISTINGOpens the file. The function fails if the file is not existing. (Default)
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 will be truncated and overwritten.
FA_OPEN_ALWAYSOpens the file if it is existing. If not, a new file will be created.
FA_OPEN_APPENDSame as FA_OPEN_ALWAYS except the read/write pointer is set end of the file.
Mode flags of POSIX fopen() corresponds to FatFs mode flags as follows:
POSIXFatFs
"r"FA_READ
"r+"FA_READ | FA_WRITE
"w"FA_CREATE_ALWAYS | FA_WRITE
"w+"FA_CREATE_ALWAYS | FA_WRITE | FA_READ
"a"FA_OPEN_APPEND | FA_WRITE
"a+"FA_OPEN_APPEND | FA_WRITE | FA_READ
"x"*1FA_CREATE_NEW | FA_WRITE
"x+"*1FA_CREATE_NEW | FA_WRITE | FA_READ
*1: glibc extension


Return Values

FR_OKFR_DISK_ERRFR_INT_ERRFR_NOT_READYFR_NO_FILEFR_NO_PATHFR_INVALID_NAMEFR_DENIEDFR_EXISTFR_INVALID_OBJECT,FR_WRITE_PROTECTEDFR_INVALID_DRIVEFR_NOT_ENABLEDFR_NO_FILESYSTEMFR_TIMEOUTFR_LOCKEDFR_NOT_ENOUGH_CORE,FR_TOO_MANY_OPEN_FILES


Description

The f_open function opens a file and creates a file
object. The file object is used for subsequent read/write operations to the file to identify the file. Open file should be closed with f_close function
after the session of the file access. If any change to the file is made and not closed prior to power down, media removal or re-mount, or the file can be collapsed.

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


QuickInfo

Always available. Only FA_READ and FA_OPEN_EXISTING are
supported when FF_FS_READONLY == 1.


Example

/* Read a text file and display it */

FATFS FatFs;   /* Work area (filesystem object) for logical drive */

int main (void)
{
FIL fil;        /* File object */
char line[100]; /* Line buffer */
FRESULT fr;     /* FatFs return code */

/* Register work area to the default drive */
f_mount(&FatFs, "", 0);

/* Open a text file */
fr = f_open(&fil, "message.txt", FA_READ);
if (fr) return (int)fr;

/* Read all lines and display it */
while (f_gets(line, sizeof line, &fil)) {
printf(line);
}

/* Close the file */
f_close(&fil);

return 0;
}

/* Copy a file "file.bin" on the drive 1 to drive 0 */

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

/* Register work area for each logical drive */
f_mount(&fs[0], "0:", 0);
f_mount(&fs[1], "1:", 0);

/* Open source file on the drive 1 */
fr = f_open(&fsrc, "1:file.bin", FA_READ);
if (fr) return (int)fr;

/* Create destination file on the drive 0 */
fr = f_open(&fdst, "0:file.bin", FA_WRITE | FA_CREATE_ALWAYS);
if (fr) return (int)fr;

/* Copy source to destination */
for (;;) {
fr = f_read(&fsrc, buffer, sizeof buffer, &br);  /* Read a chunk of source file */
if (fr || br == 0) break; /* error or eof */
fr = f_write(&fdst, buffer, br, &bw);            /* Write it to the destination file */
if (fr || 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(NULL, "0:", 0);
f_mount(NULL, "1:", 0);

return (int)fr;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: