我不熟悉 C。我必须使用函数从 .csv 文件中读取值到三个不同的数组。函数原型(prototype)如下所示:
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);
}
这个想法是在函数readUsageFromFile中使用函数csvToString()。我已经看到了如何从 .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 行。
您需要:
- 逐行读取 csv
- 对于除标题之外的每一行
- 使用
csvToStrings
获取该行中的每个值 - 根据需要使用这些值
- 使用
关于c - 将c中的.csv文件读入多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46546548/