0

I have an issue with a CSV writing function, some times it prints a blank line to the file. How do I check for this or remove it as I dont want to write a blank line to the CSV. My CSV output is:

CSV output

As you can see the first 2 lines are correct the third is an error After some time it recovers and the extra lines are removed.

Here is my Write function to the 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;
}

I have a set of internal char arrays which store the data values to write to the CSV, I simply loop them and store to the SD card. But, I need to not create the blank lines.

Thomas Morris
  • 794
  • 5
  • 26
  • I doubt it has to do with all the sd card stuff. In the `printf` statement which globbers all the single strings into one big string buffer, you insert an additional end of line after each row into the output stream. However, what happens if there was already an end of line somwhere in the input? E.g. in the statement `snprintf(buffer, sizeof buffer, "%s,%s,%s,%s,%s,%s\r\n",stUT,stT1,stT2,stT3,stT4,stPd);` one of the char arrays may accidently contain already an end of line. – Wör Du Schnaffzig May 19 '20 at 11:30
  • Possibly not related: your `if((SD_CARD_T1[i] != 0) &&...` is fishy. If it is false, then nothing is written, yet the next statement checks `ff_result `. – Paul Ogilvie May 19 '20 at 11:35
  • That is probably it but how can I check that line before I write it? – Thomas Morris May 19 '20 at 11:41

0 Answers0