【问题标题】:Reading in .csv File in c into multiple variables将c中的.csv文件读入多个变量
【发布时间】:2017-10-03 14:05:01
【问题描述】:

我不熟悉 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);
}

想法是在函数readUsageFromFile中使用函数csvToString()。我已经看到了如何从 .csv 文件中读取值的示例,但我对如何将值读取到不同的数组并将其分配给正确的数组感到困惑。我该如何开始呢?

【问题讨论】:

  • stackoverflow.com/questions/32349263/… 我发布了一个基本的 CSV 解析器。这可能更简单。
  • “我该如何开始呢?”在这个网站上不是一个好问题。这不是一个辅导网站,也不是为您编写代码。当你准备好去尝试一些东西但它不起作用时,我们可以帮助你。
  • 写给你csvToStrings函数的人应该添加了它的描述和它的参数。
  • 当您传递一个数组时,您还必须在该函数中提供它的大小。
  • 对不起,我不想让别人为我写代码我只是想要一个提示,我迷路了,已经尝试了几个网站。我对此的笔记非常有限。

标签: c arrays csv


【解决方案1】:

我不会为你解决问题。但是我要做的是写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 从该行获取每个值
    • 根据需要使用值

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 2021-04-16
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2017-03-05
    • 2014-12-09
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多