2024-05-10 04:16 AM
Hello everyone,
Thanks in advance.
I create a .txt file and write data in it. When this .txt file reaches 1GB, I want it to open a new .txt file and write the data to the new .txt file. It should continue from where it left off...
I have 2 folders, one with configuration information and the other with data saved.
I tried many ways but I could not solve the problem.
Which path should I follow?
void check_or_create_initial_setting(void) {
UINT br;
// FRESULT fr;
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
// Check if the "System" folder exists
if (f_opendir(&dir, "/System")== FR_OK) {
// The "system" folder exists, now check for "config.txt"
if (f_open(&SDFile, "/System/Config.txt", FA_READ)== FR_OK) {
// The "config.txt" file exists, read and extract the coefficient value
while (f_gets(line, sizeof(line), &SDFile)) {
if (sscanf(line, "coefficient = %d", &multiplication) == 1) {
// Coefficient value found and extracted
break;
}
}
f_close(&SDFile); // Close the file after reading
} else {
// "config.txt" does not exist, create it and write "coefficient = 12"
if (f_open(&SDFile, "/System/config.txt", FA_CREATE_NEW | FA_WRITE)== FR_OK) {
char *configData = "coefficient = 12";
f_write(&SDFile, configData, strlen(configData), &br);
f_close(&SDFile); // Close the file after writing
}
}
f_closedir(&dir); // Close the directory
} else {
// "system" folder does not exist, create it
f_mkdir("/System");
// "config.txt" does not exist, create it and write "coefficient = 12"
if (f_open(&SDFile, "/System/config.txt", FA_CREATE_NEW | FA_WRITE)== FR_OK) {
char *configData = "coefficient = 12";
f_write(&SDFile, configData, strlen(configData), &br);
f_close(&SDFile); // Close the file after writing
}
f_closedir(&dir); // Close the directory
}
}
// Unmount the file system
// f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
f_mount(NULL, "", 0);
}
void create_and_write_to_file(void) {
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
else
{
// Check if the data folder exists
if (f_opendir(&dir, "/Data") != FR_OK) {
// Folder doesn't exist, create it
f_mkdir("/Data");
} else {
f_closedir(&dir);
}
//char path[32] = "/Data/Data";
int maxNumber = 0;
// Open the directory
if (f_opendir(&dir, "/Data") == FR_OK) {
// Read directory items
while (f_readdir(&dir, &fno) == FR_OK && fno.fname[0]) {
// Check if it's a file
if (!(fno.fattrib & AM_DIR)) {
// Check if the file name matches the pattern (e.g., data1.txt)
int num;
if (sscanf(fno.fname, "Data%d.txt", &num) == 1) {
if (num > maxNumber) {
maxNumber = num; // Update max number
}
}
}
}
f_closedir(&dir);
}
// Create new file with incremented name
sprintf(newFileName, "/Data/Data%d.txt", maxNumber + 1);
if (f_open(&SDFile, newFileName, FA_OPEN_APPEND | FA_WRITE) == FR_OK) {
while (SWICH==1) {
f_write(&SDFile, sd_buf_write_char, 10240, &byteswritten);
while (iindex < sizeof(sd_buf_write_char) - 1 && cont0 < 65535) {
int written = snprintf(&sd_buf_write_char[iindex], sizeof(sd_buf_write_char) - iindex, "%d", cont0);
if (written > 0) {
iindex += written; // Advance the index by the number of characters written
cont0++; // Increment the counter
} else {
// Handle error
break;
}
}
}
f_close(&SDFile);
}
}
f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
}
2024-05-10 04:21 AM - edited 2024-05-10 04:26 AM
Simply keep a count each time you write to the file?
The FatFs f_write() function tells you how much it actually wrote:
http://elm-chan.org/fsw/ff/doc/write.html
FatFs also has an f_size() function - it tells you the current size of an existing file:
http://elm-chan.org/fsw/ff/doc/size.html
You can also get the size of an existing file via directory functions; eg,
http://elm-chan.org/fsw/ff/doc/readdir.html