I have the following function in 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);
}
However I need a temporary variable to keep old value and then return &(fno->fname)
. I created local variable FILINFO t_fno
and then pass it to function res = f_readdir(dir, &t_fno);
everything is OK.
But if I wanted to use t_fno as local pointer and then pass it to f_readdir I don't get actual data but some random memory readings and program crashes on if (t_fno->fname[0] == '\0')}
. I tried also initalizing FILINFO* t_fno = NULL
but this doesn't help.
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);
}