cancel
Showing results for 
Search instead for 
Did you mean: 

f_open directory name from variable

Lori B.
Associate III
Posted on September 18, 2014 at 11:32

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 #file
4 REPLIES 4
Posted on September 18, 2014 at 13:43

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 18, 2014 at 14:05

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.

Lori B.
Associate III
Posted on September 18, 2014 at 15:09

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!

Posted on September 18, 2014 at 18:59

That's good !

Cheers,

Heisenberg.