将c中的.csv文件读入多个变量
我不熟悉C。我必须使用函数从.csv文件中读入三个不同数组的值。函数原型如下所示:将c中的.csv文件读入多个变量,c,arrays,csv,C,Arrays,Csv,我不熟悉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 ... 第一个值是天,第二个值是一天中的时间,第三个值
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);
}
/*
* 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);
其思想是在函数readUsageFromFile中使用函数csvToString()。我已经看到了如何从.csv文件中读入值的示例,但对于如何将值读入不同的数组并将其分配给正确的数组,我感到困惑。我如何开始呢?我不会为您解决这个问题。然而,我要做的是编写
csvToStrings
函数的人首先应该做的事情:我要正确地记录函数:
#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);
}
/*
* 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*
- 每个输出缓冲区都应该有一个size参数。 函数应检查越界访问
- 函数应该验证输入
- 解析可以改进。强烈建议使用字符串库函数代替原始
,而
循环和逐字符手动复制
- 逐行读取csv
- 除页眉外的每行
- 使用
从该行获取每个值csvToStrings
- 根据需要使用这些值
- 使用
csvToStrings
函数的人应该添加对该函数及其参数的描述。当您传递数组时,你必须提供它的大小以及在该功能。我很抱歉,我不想有人为我写的代码我只是想得到一个提示,我迷路了,并尝试了几个网站。我在这方面的笔记非常有限。