2014-09-18 02:32 AM
I'm using a STM32F4 microcontroller witch FatFS library and I'm trying to write a file on an USB OTG drive. I'm using the f_open f
unction and it works fine like that
if(f_open(&fileW, ''0:\\Folder\\INFO.TXT'',FA_CREATE_ALWAYS | FA_WRITE) == FR_OK){ ...writing the file...
}
What I want now is to write the file in a folder which name is in a variable.
This is what my code looks like now:
int len = strlen(FolderName)+strlen(''0:\\\\INFO.TXT'')+1;
char PathString[len];
snprintf(PathString,len,''0:\\%s\\INFO.TXT'', FolderName);
TCHAR str[len], *t = str;
char *s = PathString;
while(*s)
*t++ = (TCHAR)*s++;
*t = 0;
if(f_open(&fileW, str, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)
{ ..writing..
}
..but it's not working. If I remove the ''\\'' from the path name, I can create a file named FolderNameINFO.txt in the main root, so the conversion TCHAR and the f_open function seem to work. But when I add the ''\\'' to create a folder path, I'm not able to go into the if condition.
What am I doing wrong? Thank you!
#f_open #fatfs #stm32 #usb #file2014-09-18 04:43 AM
Does the folder actually exist before you create the file? There are other functions to create folders, and you should probably use those and others to decompose the path, and create any folders as required.
2014-09-18 05:05 AM
Hi lori.b,
You have just to use the f_mkdir() FatFs API to create the directory, since the f_open() API in FA_CREATE_ALWAYS | FA_WRITE mode, can just create and open a new file object. Regards,Heisenberg.2014-09-18 06:09 AM
I can't belive the solution was that easy.
I always used the f_open function alone, even for creating folder that don't exist. I don't know why this time, using a variable for the path, is different but it was sufficient to add a function to create the folder before creating the file, and now it works. Thank you and sorry for the trivial problem!2014-09-18 09:59 AM
That's good !
Cheers,Heisenberg.