This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I cannot create subfolders in FATFS

Hi, I'm trying to create subfolders with loop. My partly code looks like:

char *str = "dirA/dirB/dirC/file.txt";
char *token = strtok(str, "/");

static FILINFO fno;
FRESULT fr_res;

while(token != NULL)
{
	if (f_stat(token, &fno) != FR_OK)
	{
		fr_res = f_mkdir(token);
		if (fr_res != FR_OK) {printf("Can't create dir\r\n");}
	}
	if (f_chdir(token) != FR_OK)
	{
		printf("Can't change dir\r\n");
	}
	token = strok(NULL, "/");
	printf("Current token is %s\r\n", token);
}

So, it creates new dir but after that my app is crashing and rebooting, and it works fine if all dirs and subdirs are created (if dirA, dirB, dirC already exist) What I do wrong? Thanks in advanced

Parents Reply Children
  • Hi,

    As I read it is basically not recommended to use strtok function due to some drawbacks. If possible you should use strtok_r.

    One of the drawbacks of strtok is that this function is modyfing its first argument. When you are trying to modify text pointed by char * str, most likely you are trying modify const variable (written in Flash).

    When you will use an array you will not have this problem as string will be in RAM.

Related