我在 C 中有以下功能:
char* AUDIO_GetFile(FIL* fil, DIR* dir, FILINFO* fno) {
FRESULT res;
res = f_readdir(dir, fno); /* Read a directory item */
if (fno->fname[0] == '\0')} /* End of files */
...
}
...
return &(fno->fname);
}
但是我需要一个临时变量来保留旧值然后返回&(fno->fname)
。我创建了局部变量FILINFO t_fno
,然后将其传递给函数,res = f_readdir(dir, &t_fno);
一切正常。
但是,如果我想使用 t_fno 作为本地指针,然后将其传递给 f_readdir,我不会得到实际数据,而是会在if (t_fno->fname[0] == '\0')}
. 我也尝试过初始化FILINFO* t_fno = NULL
,但这没有帮助。
char* AUDIO_GetFile(FIL* fil, DIR* dir, FILINFO* fno) {
FRESULT res;
FILINFO* t_fno;
res = f_readdir(dir, t_fno); /* Read a directory item */
if (t_fno->fname[0] == '\0')} /* crash */
...
}
...
fno = t_fno;
return &(fno->fname);
}
FRESULT f_readdir (
DIR* dp, /* Pointer to the open directory object */
FILINFO* fno /* Pointer to file information to return */
)
{
FRESULT res;
DEFINE_NAMEBUF;
res = validate(dp); /* Check validity of the object */
if (res == FR_OK) {
if (!fno) {
res = dir_sdi(dp, 0); /* Rewind the directory object */
} else {
INIT_BUF(*dp);
res = dir_read(dp, 0); /* Read an item */
if (res == FR_NO_FILE) { /* Reached end of directory */
dp->sect = 0;
res = FR_OK;
}
if (res == FR_OK) { /* A valid entry is found */
get_fileinfo(dp, fno); /* Get the object information */
res = dir_next(dp, 0); /* Increment index for next */
if (res == FR_NO_FILE) {
dp->sect = 0;
res = FR_OK;
}
}
FREE_BUF();
}
}
LEAVE_FF(dp->fs, res);
}