簡體   English   中英

將c中的.csv文件讀入多個變量

[英]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 行。

你需要:

  • 逐行讀取 csv
  • 對於除標題之外的每一行
    • 使用csvToStrings從該行獲取每個值
    • 根據需要使用值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM