使用 f_mount 读写数据到文本文件

Using f_mount to read and write data to text file

在我的应用程序中,我需要使用调用 f_open、f_read 和 f_write.

打开、读取数据并将数据写入文本文件

无法打开 .txt 文件

res = f_open(&f_header.file, file_path, FA_OPEN_EXISTING | FA_WRITE | FA__WRITTEN | FA_READ | FA_CREATE_NEW  );
                printf("res value after f open %d \n\r",res);
if (res != FR_OK) {
                printf("Failed to open %s, error %d\n\r", file_path, res);
    }

这是错误:

FR_NOT_ENABLED, /* (12) The volume has no work area */

为了解决这个错误,应用程序需要在每次媒体更改后执行 f_mount 功能以强制清除文件系统对象。

如何在此应用程序中使用 f_mount() 调用来解决此问题? 我不清楚第二个参数。

我添加了这个 f_mount(&fs0, "0://", 1);解决这个问题。

在 f_open 调用之前。它也没有接 f_mount() 电话。

res=f_mount(&fs0,"0://", 1);

res = f_open(&f_header.file, file_path, FA_OPEN_EXISTING | FA_WRITE | FA__WRITTEN | FA_READ | FA_CREATE_NEW  );

->代码在 f_mount()*

之前 运行 时间停止

这是我正在使用的 f_mount 的源代码:

FRESULT f_mount (
    FATFS* fs,          /* Pointer to the file system object (NULL:unmount)*/
    const TCHAR* path,  /* Logical drive number to be mounted/unmounted */
    BYTE opt            /* 0:Do not mount (delayed mount), 1:Mount immediately */
)
{
    FATFS *cfs;
    int vol;
    FRESULT res;
    const TCHAR *rp = path;
    vol = get_ldnumber(&rp);
    if (vol < 0) return FR_INVALID_DRIVE;
    cfs = FatFs[vol];                   /* Pointer to fs object */
    if (cfs) {
#if _FS_LOCK
        clear_lock(cfs);
#endif
#if _FS_REENTRANT                       /* Discard sync object of the current volume */
        if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
#endif
        cfs->fs_type = 0;               /* Clear old fs object */
    }
    if (fs) {
        fs->fs_type = 0;                /* Clear new fs object */
#if _FS_REENTRANT                       /* Create sync object for the new volume */
        if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
#endif
    }
    FatFs[vol] = fs;            /* Register new fs object */
    if (!fs || opt != 1) return FR_OK;  /* Do not mount now, it will be mounted later */
    res = find_volume(&fs, &path, 0);   /* Force mounted the volume */
    LEAVE_FF(fs, res);
}

提前致谢和问候

根据http://elm-chan.org/fsw/ff/doc/mount.html

FRESULT f_mount (
  FATFS*       fs,    /* [IN] Filesystem object */
  const TCHAR* path,  /* [IN] Logical drive number */
  BYTE         opt    /* [IN] Initialization option */
);

Parameters
  fs
    Pointer to the filesystem object to be registered and cleared. Null pointer unregisters the registered filesystem object.
  path
    Pointer to the null-terminated string that specifies the logical drive. The string without drive number means the default drive.
  opt
    Mounting option. 0: Do not mount now (to be mounted on the first access to the volume), 1: Force mounted the volume to check if it is ready to work.

换句话说,第二个参数是您希望在以后使用该特定文件系统时如何引用它。

比如这样挂载:

f_mount(&fs0, "0://", 1);

然后您将能够像这样打开文件:

f_open(fp, "0://path/to/file", FA_CREATE_ALWAYS);