gpt4 book ai didi

c - FatFS:f_findfirst 返回一个与找到的文件不对应的字符串

转载 作者:行者123 更新时间:2023-11-30 14:34:51 28 4
gpt4 key购买 nike

我正在使用 FatFs 库在 SD 卡上创建和管理文件系统。目标是识别包含引导加载程序更新(或不更新)设备的固件的文件。

我遵循的程序如下:

DIR  directory;
FILINFO filinfo;

f_findfirst(&directory, &filinfo, "", "firmware_");

据我了解,应识别以 "firmware_" 开头的第一个文件,并将信息存储在 filinfo 中。这确实有效,但是当我尝试从 filinfo.fname 中提取名称时,已提取的名称如下:"\0IRMWA~1.BIN".

我不知道为什么以这种格式提取文件的命名。我们的想法是稍后获取文件的第二部分:“firmware_01_01_01”并执行 char 到 int 的转换来处理版本号,但我无法提取文件名的完整字符串。

最佳答案

我也遇到了类似的问题。这是我的代码。希望您觉得这有帮助

#define FW_FOLDER               "fw"
#define FW_PATH "0:/" FW_FOLDER
#define FW_BIN "*.bin"

FRESULT res;
DIR dir;
FILINFO fno;
static char fw_path[128];

// Find first item
res = f_findfirst(&dir, &fno, FW_PATH, FW_BIN);

// Create directory (optional)
if (res == FR_NO_PATH)
{
printf("Creating %s directory!\n", FW_FOLDER);
f_mkdir(FW_FOLDER);
}

// Repeat until proper item is found
while (res == FR_OK)
{
// Make sure file name is valid
if (fno.fname[0] == 0)
break;

// Debugging
printf("Scanning: %s\n", fno.fname);

// Make sure it is not a directory
if (!(fno.fattrib & AM_DIR))
{
// Success!
printf("Found: %s\n", fno.fname);

// Construct file path
sprintf(fw_path, "%s/%s", FW_PATH, fno.fname);

break;
}

// Move to the next one
res = f_findnext(&dir, &fno);
}

// Close directory
f_closedir(&dir);

是的,您需要在 fatfs 库中启用 FF_USE_FIND == 1。如果您决定将 2 用于 FF_USE_FIND,则必须检查 altname 作为 fno 的一部分。

此外,我最终必须将文件验证更新到此代码段

// Make sure file name is alpha numeric
if (!isalnum(fat_fno.fname[0]))
break;

关于c - FatFS:f_findfirst 返回一个与找到的文件不对应的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58798894/

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