[英]Reading in .csv File in c into multiple variables
我不熟悉 C。我必须从带有函数的 .csv 文件中读取三个不同数组的值。 函数原型如下所示:
void readUsageFromFile(double usage1[], double usage2[], double usage3[]);
.csv 文件采用以下格式:
Day,Time,Apartment1,Apartment2,Apartment3
01,00:00,0,0.001,0
01,01:00,0,0,0
01,02:00,0,0,0
...
第一个值是天,第二个值是一天中的时间,第三个值是第一间公寓的用水量,第四个值是第二间公寓的,第五个值是第三间公寓的。 这些值代表每间公寓每天每小时的用水量,持续 30 天,因此有 720 行值
所有这些值都在一个列中,有 721 行,包括标题Day、Time、Apartment1、...
我还获得了一个可用于处理 .csv 文件的函数,但它只会让我更加困惑。 这是函数:
#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);
}
我们的想法是使用功能csvToString()的函数readUsageFromFile。 我已经看到了如何从 .csv 文件中读取值的示例,但我对如何将值读取到不同的数组并将其分配给正确的数组感到困惑。 我怎样才能开始呢?
我不会为你解决问题。 但是,我将做编写csvToStrings
函数的人首先应该做的事情:我将正确记录该函数:
/*
* 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);
附带说明一下,该函数有几个问题:
csvString
应该是const char*
while
循环和逐字符手动复制。如您所见,该函数解析 CSV 行。
你需要:
csvToStrings
从该行获取每个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.