我对 CSV 写入功能有疑问,有时它会在文件中打印一个空行。如何检查或删除它,因为我不想在 CSV 中写入空行。我的 CSV 输出是:
如您所见,前两行是正确的,第三行是错误的,经过一段时间后它恢复并删除了多余的行。
这是我对 CSV 的写入函数:
void Write_block_to_sd_card()
{
while (fno.fname[0]);
ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
if(ff_result != FR_OK)//Not passing if the file is missing
{
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
SD_CARD_PRESENT = 0;
return;
}
}
else//File was openned fine
{
NRF_LOG_RAW_INFO("");
NRF_LOG_INFO("Writing to file as a block " FILE_NAME "...");
SD_CARD_PRESENT = 1;
//File is opened
for(int i = 0; i<MAXIMUM_BUFFER_STORAGE;i ++)
{
char buffer[50];//Buffer to hold output
char stT1 [MAX_BUFFER_SIZE];//T1
char stT2 [MAX_BUFFER_SIZE];//T2
char stT3 [MAX_BUFFER_SIZE];//T3
char stT4 [MAX_BUFFER_SIZE];//T4
char stPd [MAX_BUFFER_SIZE];//Pdiff
char stUT [MAX_BUFFER_SIZE];//Unix time
sprintf(stT1, "%d", SD_CARD_T1[i]);
sprintf(stT2, "%d", SD_CARD_T2[i]);
sprintf(stT3, "%d", SD_CARD_T3[i]);
sprintf(stT4, "%d", SD_CARD_T4[i]);
sprintf(stPd, "%f", SD_CARD_Pdiff[i]);
sprintf(stUT, "%d", SD_CARD_UNIX_TIME[i]);
//Do not write the ID to the SD card only chuck over the Unix time
snprintf(buffer, sizeof buffer, "%s,%s,%s,%s,%s,%s\r\n",stUT,stT1,stT2,stT3,stT4,stPd);//Convert to a string
if (strcmp(buffer, "") != 0)//Not null
{
if((SD_CARD_T1[i] != 0) && (SD_CARD_T2[i] != 0) && (SD_CARD_T3[i] != 0) && (SD_CARD_T4[i] != 0))//Null check
//Need to always write the correct length of data
ff_result = f_write(&file, buffer, sizeof(buffer), (UINT *) &bytes_written);//Add the input data to the CSV file
if (ff_result != FR_OK)
{
NRF_LOG_INFO("Write failed\r\n.");
}
else
{
}
}
ID = ID + 1;//Increment the ID counter
}
NRF_LOG_INFO("Write new entry %d bytes written.", (bytes_written*(MAXIMUM_BUFFER_STORAGE)));
}
(void) f_close(&file);
Time_stamp_id = ID;//Set the time stamp id to the latest value.
return;
}
我有一组内部 char 数组,它们存储要写入 CSV 的数据值,我只是将它们循环并存储到 SD 卡中。但是,我不需要创建空行。