cancel
Showing results for 
Search instead for 
Did you mean: 

How do I write multiple lines for multiple times in a text file using Cube Mx & SD FatFS library?

MPate.7
Associate

Hi,

I am using cube mx FatFS library for Data Logging in a SD card using STM32F401 board.

The examples available on internet, are only writing one time in the text file. I have tried \r and \n in code for new line, But each time I write it starts from line one.

Here is my code

FATFS SD_FATFS;

FIL MyFile;

unsigned int TestByte;

char FilePath[] = "Write1.txt";

char Data[] = "Hello World!";

char Data1[100];

while (1)

 {

HAL_Delay(2000);

HAL_GPIO_TogglePin(LED_ORANGE_PORT,LED_ORANGE_PIN);

if(f_open(&MyFile, FilePath, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)

HAL_GPIO_WritePin(LED_RED_PORT,LED_RED_PIN,GPIO_PIN_SET);

sprintf((char *) Data1,(char *)"\r\n");

if( f_write(&MyFile,Data1,sizeof(Data1),&TestByte) != FR_OK)

HAL_GPIO_WritePin(LED_RED_PORT,LED_RED_PIN,GPIO_PIN_SET);

if(f_write(&MyFile,Data,sizeof(Data),&TestByte) != FR_OK)

HAL_GPIO_WritePin(LED_RED_PORT,LED_RED_PIN,GPIO_PIN_SET);

if(f_close(&MyFile) != FR_OK)

HAL_GPIO_WritePin(LED_RED_PORT,LED_RED_PIN,GPIO_PIN_SET);

 }

Please help

Thanks in advance

1 REPLY 1

Hi

Don't use FA_CREATE_ALWAYS flag. This flag "Creates a new file. If the file is existing, it will be truncated and overwritten."

Use FA_OPEN_ALWAYS flag to for sequentially write data.