简体   繁体   中英

Reading in .csv File in c into multiple variables

I am unfamiliar with C. I have to read in values to three different arrays from a .csv file with a function. The function prototype looks like this:

void readUsageFromFile(double usage1[], double usage2[], double usage3[]);

The .csv file is in the following format:

Day,Time,Apartment1,Apartment2,Apartment3
01,00:00,0,0.001,0
01,01:00,0,0,0
01,02:00,0,0,0
...

The first value is the day, the second value is the time of day, the third is the water usage for the first apartment, the forth value is for the second apartment and the fifth for the third apartment. The values represent the water usage of each apartment for every hour of a day for 30 days, therefore 720 rows of values

All these values are in a single column and there are 721 rows including the headers Day,Time,Apartment1,...

I am also given a function that I can use to process the .csv file but it only confuses me more. Here is the function:

#define DEBUG 0


void csvToStrings(char *csvString, char *day, char *time, char *usage1, char 
*usage2, char *usage3)
{
    // Declare variables
    int i, j;
    char c;

    if (DEBUG)
        printf("csvToStrings: Length of string is %d\n", strlen(csvString));

    // Read day
    i = 0;
    j = 0;
    c = csvString[i++];
    while (c != ',')
    {
        day[j++] = c;
        c = csvString[i++];
    }
    day[j++] = '\0';
    if (DEBUG)
        printf("csvToStrings: day string: %s\n", day);

    // Read time
    j = 0;
    c = csvString[i++];
    while (c != ',')
    {
        time[j++] = c;
        c = csvString[i++];
    }
    time[j++] = '\0';
    if (DEBUG)
        printf("csvToStrings: time string: %s\n", time);

    // Read usage1
    j = 0;
    c = csvString[i++];
    while (c != ',')
    {
        usage1[j++] = c;
        c = csvString[i++];
    }
    usage1[j++] = '\0';
    if (DEBUG)
        printf("csvToStrings: usage1 string: %s\n", usage1);

    // Read usage2
    j = 0;
    c = csvString[i++];
    while (c != ',')
    {
        usage2[j++] = c;
        c = csvString[i++];
    }
    usage2[j++] = '\0';
    if (DEBUG)
        printf("csvToStrings: usage2 string: %s\n", usage2);

    // Read usage3
    j = 0;
    c = csvString[i++];
    while (c != '\0')
    {
        usage3[j++] = c;
        c = csvString[i++];
    }
    usage3[j++] = '\0';
    if (DEBUG)
        printf("csvToStrings: usage3 string: %s\n", usage3);
}

The idea is to use the function csvToString() in the function readUsageFromFile . I have seen examples of how to read in the values from a .csv file but I am confused on how to read in the values to different arrays and assign it to the correct array. How can I get started on this ?

I am not going to resolve the problem for you. However I am going to do what the one who wrote csvToStrings function should have done in the first place: I am going to properly document the function:

/*
 * Function to parse a csv line
 * 
 * The function reads the csv line from `csvString` parameter
 * and writes the values to the `day`, `time`, `usage1`, `usage2` and `usage3` parameters
 *
 *
 * Parameters:
 *    csvString - C string. Input. Mandatory
 *        a CSV line. A comma (,) separated list of values
 *        respecting the format:
 *        Day,Time,Apartment1,Apartment2,Apartment3
 *
 *    day - string buffer. output. Mandatory
 *        a preallocated buffer to write the parsed day value to.
 *        the buffer must have a size to fit
 *        the value including the null terminating character
 *
 *    time - string buffer. output. Mandatory
 *        a preallocated buffer to write the parsed time value to.
 *        the buffer must have a size to fit
 *        the value including the null terminating character
 *
 *    usage1-3 - string buffer. output. Mandatory
 *        3 preallocated buffers to write the parsed usage1-3 values to.
 *        the buffers must have a size to fit
 *        the values including the null terminating character
 *
 *        
*/
void csvToStrings(char *csvString, char *day, char *time, char *usage1,
                  char *usage2, char *usage3);

On a side note, the function has several issues:

  • csvString should be const char*
  • each of the output buffer should have a size parameter to them. The function should check for out of bounds access.
  • The function should validate the input
  • the parsing could be improved. Usage of string library functions instead of raw while loops and character-by-character manual copies strongly suggested.

What you have to do

As you can see, the function parses a CSV line.

You need to:

  • read the csv line by line
  • for each line except the header
    • use csvToStrings to get each value from that line
    • use the values as you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM