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 ?