Skip to main content
FPicc.1
Associate III
Feb 15, 2023 6:04 PM UTC
Solved

How to edit an existing .csv file in the SD card?

I want to save constant data on .csv files in an SD card. I managed to create the file, but when I call the function to edit, it seems not to recognize the end of the file to start writing. How can I do that? Or what can I do different? That's the part where it creates and edits the file, and the f_lseek function it's supposed to find the end of the file. Also, I'm running in a 4096 size buffer and using fatfs_sd.h library.

fresult = f_open(&fil, "Test_Data_13-02.csv", FA_OPEN_ALWAYS | FA_WRITE);
	 fresult = f_lseek(&fil, filinfo.fsize);
	 fresult = f_puts("------------------- New Test -------------------\n", &fil);
 
	 for(test= 0; test < 10;test++)
	 {
		 for(test2 = 10; test2 < 20; test2++)
		 {
			 fresult = f_printf(&fil, "%d,%d,\n", test, test2);
			 HAL_Delay(100);
		 }
		 HAL_Delay(100);
	 }
 
	 	fresult = f_close(&fil);
	 	//Open to read
	 fresult = f_open(&fil, "Test_Data_13-02.csv", FA_READ);
	 //Read string from the file
	 f_read(&fil, buffer, filinfo.fsize, &br);
	 f_close(&fil);
	 bufclear();

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Feb 15, 2023 7:58 PM UTC

Ok, you're using an antiquated version of FATFS that ST bundles and hasn't updated in years.

Use the f_size() macro, it is in ff.h

fresult = f_lseek(&fil, f_size(&fil));

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..

5 replies

Tesla DeLorean
Guru
Feb 15, 2023 6:43 PM UTC

Use the structure/instance you're opening, ie fil not filinfo

https://community.st.com/s/question/0D50X0000BsNyUMSQ0/how-to-use-faopenappend-in-sd-card

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
FPicc.1
FPicc.1Author
Associate III
Feb 15, 2023 6:46 PM UTC

But there's no .fsize in the FIL struct, only in the FILINFO. Do I just switch them?

P.S.: didn't work

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Feb 15, 2023 7:58 PM UTC

Ok, you're using an antiquated version of FATFS that ST bundles and hasn't updated in years.

Use the f_size() macro, it is in ff.h

fresult = f_lseek(&fil, f_size(&fil));

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..