gpt4 book ai didi

c - FatFS f_write 不工作

转载 作者:行者123 更新时间:2023-12-02 00:55:09 26 4
gpt4 key购买 nike

我在 ZedBoard 上,我想在 SD 卡上写。我在嵌入式系统上工作,我没有任何操作系统,我正在运行裸机。

我可以从 SD 卡上正常读取,它们没问题。

但是当我尝试阅读时,我有一些奇怪的行为:

  • f_write 返回 FR_OK
  • bw(写入的字节数)变量是正确的
  • 文件已创建(我从我的PC读取SD卡时可以看到它)

但是当我读取文件时,它是空的。

这是我的代码:

void WriteFile(char const* fileName, char* buffer, size_t size)
{
FIL file;
FATFS fs;
UINT bw;
FRESULT fr;

f_mount(&fs, "", 0);
f_open(&file, fileName, FA_WRITE | FA_CREATE_ALWAYS);
fr = f_write(&file, buffer, size, &bw);
if (size != bw || fr != FR_OK)
PRINT(("Error in writing !\n"));
f_close(&file);
f_mount(NULL, "", 0);
}

我这样调用方法:

 WriteFile("Hello.txt", "Hello World !", 13);

知道我做错了什么吗?

最佳答案

您可以直接刷新缓冲区 (f_sync):

        FRESULT result;
FATFS fs;
FIL file;

const char string[] = "Hallo world\0";
uint16_t written = 0;
FILINFO fno;

/* Open or create a log file and ready to append */
printf("mount:%d",f_mount(&fs, "", 1));
printf("open: %d", f_open(&file, "log.txt", FA_WRITE | FA_OPEN_ALWAYS ));
printf("stat: %d", f_stat("log3.txt", &fno));
printf("write: %d wr: %d", f_write(&file, string, 11, &written), written);
printf("flush: %d", f_sync(&file));
printf("close: %d", f_close(&file)); /* close performs sync before close */

关于c - FatFS f_write 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com