cancel
Showing results for 
Search instead for 
Did you mean: 

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

FPicc.1
Senior

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();

1 ACCEPTED SOLUTION

Accepted Solutions

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
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

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
Up vote any posts that you find helpful, it shows what's working..

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

P.S.: didn't work

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
Up vote any posts that you find helpful, it shows what's working..

Alright, it worked! Thanks!

But why is my library antiquated? It is generated automatically, and I've downloaded the latest version of CubeIDE.

I'm using NUCLEO stm32wl55jc1, bytheway.

From 2017 and broken, by using things known to be broken from packages known not to be updated in a timely fashion, you get to repeat failure endlessly... and it gets duplicated and spread everywhere..

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