Open BurnerK opened 1 year ago
Does the directory path
already exist on the SD card when you are calling fopen("/sdcard/path/123.txt", "w")
?
Does the directory
path
already exist on the SD card when you are callingfopen("/sdcard/path/123.txt", "w")
?
no exist
In that case, the behavior makes sense — it works the same way if you use fopen
on Linux or Windows with usual filesystems (FAT32, exFAT, ext4,...). fopen
doesn't automatically create subdirectories. You need to call mkdir
yourself to create it.
Note that the behavior is different with SPIFFS filesystem. SPIFFS doesn't have a concept of directories. When you are opening the file /spiffs/path/123.txt
, then it simply creates a file with the name /path/123.txt
— forward slashes are part of the filename. So if you were to do the same thing with SPIFFS, it would work without creating a subdirectory first. In fact, mkdir
with SPIFFS always returns -1 and sets errno = ENOTSUP.
In that case, the behavior makes sense — it works the same way if you use
fopen
on Linux or Windows with usual filesystems (FAT32, exFAT, ext4,...).fopen
doesn't automatically create subdirectories. You need to callmkdir
yourself to create it.Note that the behavior is different with SPIFFS filesystem. SPIFFS doesn't have a concept of directories. When you are opening the file
/spiffs/path/123.txt
, then it simply creates a file with the name/path/123.txt
— forward slashes are part of the filename. So if you were to do the same thing with SPIFFS, it would work without creating a subdirectory first. In fact,mkdir
with SPIFFS always returns -1 and sets errno = ENOTSUP.
Does this mean I can't create a directory in the SD card? In any case?
If you're using FATFS then you can use mkdir
function from C stdlib:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
...
struct stat st = {0};
if (stat("/some/directory", &st) == -1) {
mkdir("/some/directory", S_IRWXU); // S_IRWXU == 0700 permission
}
If you're using FATFS then you can use
mkdir
function from C stdlib:#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> ... struct stat st = {0}; if (stat("/some/directory", &st) == -1) { mkdir("/some/directory", S_IRWXU); // S_IRWXU == 0700 permission }
How do I use FATFS, is it by enabling SDMMC to drive the SD card?
It seems like you're already using it. You are using esp_vfs_fat_sdspi_mount
function, which mounts your SD connected through SPI as a FAT file system. It doesn't matter if you're going to connect to the SD card via SPI or SDMMC. You just need to have your SD card formatted as FAT12/FAT16/FAT32 - if not, you can do it on PC or you can leave format_if_mount_failed
set to true in this portion of your code (this probably means your SD card is already properly formatted though):
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
It seems like you're already using it. You are using
esp_vfs_fat_sdspi_mount
function, which mounts your SD connected through SPI as a FAT file system. It doesn't matter if you're going to connect to the SD card via SPI or SDMMC. You just need to have your SD card formatted as FAT12/FAT16/FAT32 - if not, you can do it on PC or you can leaveformat_if_mount_failed
set to true in this portion of your code (this probably means your SD card is already properly formatted though):esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = true, .max_files = 5, .allocation_unit_size = 16 * 1024 };
struct stat st = { 0 };
if (stat("sdcard/path", &st) == -1)
{
int ret = mkdir("sdcard/path", S_IRWXU);
printf("mkdir ret: %d!!!!\n", ret);
} else
{
printf("mkdir fail!!!!\n");
}
我使用这种方式创建文件夹,但是mkdir()
函数返回值为-1
。
I create the folder this way, but the mkdir() function returns -1.
Answers checklist.
IDF version.
v5.0.2
Operating System used.
Windows
How did you build your project?
VS Code IDE
If you are using Windows, please specify command line type.
CMD
Development Kit.
ESP32-C2
Power Supply used.
External 5V
What is the expected behavior?
Using fopen("/sdcard/path/123.txt"), a folder named
path
is normally created on the sdcard and a file named123.txt
is created in it.What is the actual behavior?
It actually crashes and reboots right when it gets to that point.
Steps to reproduce.
Debug Logs.
No response
More Information.
No response