FATFS: file name variable in f_open function

198 views Asked by At

I have question regarding using variable as file name in f_open function.

f_readdir (&mydir, &sdfileinfo)

Once f_readdir is run in debug mode, I can see my file name in fname, but I am not sure how to use file name stored in fname in f_open function.

I tried to run sdfileinfo.fname as second argument in f_open but FR_NOFILE results.

Please see the code below

f_mount(&SDFatFS, "0:", 1) ;
f_opendir (&mydir, "0:/test") ;
f_readdir (&mydir, &sdfileinfo) ;
f_open(&SDFile,sdfileinfo.fname , FA_READ |FA_OPEN_EXISTING  ) ;

enter image description here

Edit1: Please note t123.txt is file under directory test. When I run f_open(&SDFile, "0:/test/t123.txt", FA_READ) it runs successfully

1

There are 1 answers

5
Clifford On

Neither f_opendir or f_readdir change the current working directory, which will be 0:/ by default. So you are attempting to open 0:/t123.txt rather than 0:/test/t123.txt.

You need to include the path in the filename or set the cwd.

Using CWD:

f_chdir( "0:/test" ) ; // Set CWD to the target directory
f_open( &SDFile, sdfileinfo.fname, FA_READ | FA_OPEN_EXISTING ) ;

Or construct the full path (less safe and less efficient):

char path[64] = "0:/test/" ;
strcat( path, sdfileinfo.fname ) ;
f_open( &SDFile, path, FA_READ | FA_OPEN_EXISTING ) ;