Deleting files using FatFs

Hi

I have an application running on an nRF52833, developed using nRF5 SDK with Keil uVision.
It is using the FatFs filing system to save data.
To date, there has only ever been one file used – which is overwritten every time a ‘new’ file is open.
This has worked well, using f_open(), f_write(), f_read(), f_close() etc.

Now I am modifying the application to allow multiple files to be saved.
So far, I can get the application to open and close several files, and then I can successfully cycle through all the files using the f_readdir() function.

The problem I am having is in deleting a file.
When the the f_unlink() function is called, it never returns (either the code hangs or crashes).

Example:

Let’s day I want to delete “file1.txt”.

FRESULT fresult;
FILINFO f_no;
char *filename = "file1.txt";

fresult = f_stat(filename , &f_no);

fresult is FR_OK, and f_attrib is 0x20, which is the archived attribute.

So, I think I can assume (a) the file exists, (b) the file is not open, and (c) it is okay to delete it.

However if I use this code to delete the file:

FRESULT fresult;
char *filename = "file1.txt";

fresult = f_unlink(filename);

The f_unlink() function never returns - it either crashes or freezes, so I get no return code.

Related