簡體   English   中英

C 編程 - 讀取 CSV 文件

[英]C programming - Reading CSV file

我目前在從 CSV 文件讀取數據時遇到問題。

我認為代碼幾乎可以正常工作。 但是,打印的輸出顯示了一些奇怪的字符,如下所示(輸出 9-11)。

你知道這里發生了什么嗎? 我只想擺脫這些奇怪的字符,以便我可以相應地處理導入的數據。

或者,如果您對我的編碼風格有任何反饋,如果您不介意,請與我分享。

輸出:

Obsns size is 150 and feat size is 4.
1. 5.100000, 3.500000, 1.400000, 0.200000, Iris-setosa
2. 4.900000, 3.000000, 1.400000, 0.200000, Iris-setosa
3. 4.700000, 3.200000, 1.300000, 0.200000, Iris-setosa
4. 4.600000, 3.100000, 1.500000, 0.200000, Iris-setosa
5. 5.000000, 3.600000, 1.400000, 0.200000, Iris-setosa
6. 5.400000, 3.900000, 1.700000, 0.400000, Iris-setosa
7. 4.600000, 3.400000, 1.400000, 0.300000, Iris-setosa
8. 5.000000, 3.400000, 1.500000, 0.200000, Iris-setosa
9. 4.400000, 2.900000, 1.400000, 0.200000, ��L>-setosa
10. 4.900000, 3.100000, 1.500000, 0.100000, Iris���=osa
11. 5.400000, 3.700000, 1.500000, 0.200000, Iris-set��L>
12. 4.800000, 3.400000, 1.600000, 0.200000, Iris-setosa
13. 4.800000, 3.000000, 1.400000, 0.100000, Iris-setosa
14. 4.300000, 3.000000, 1.100000, 0.100000, Iris-setosa

代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int checkObsnsSize(char *dataFileName);
void readIris();

int main() {
    readIris();
    return 0;
}

void readIris() {    
    int featSize = 4;
    char *dataFileName = "iris.data";
    int obsnsSize = checkObsnsSize(dataFileName);
    float feat[featSize][obsnsSize];
    int label[obsnsSize];
    memset(feat, 0, featSize * obsnsSize * sizeof(float));
    memset(label, 0, obsnsSize * sizeof(int));

    printf("Obsns size is %d and feat size is %d.\n", obsnsSize, featSize);

    FILE *fpDataFile = fopen(dataFileName, "r");   
    if (!fpDataFile) {
        printf("Missing input file: %s\n", dataFileName);
        exit(1);
    }

    int index = 0;
    while (!feof(fpDataFile)) {
        char line[1024];
        char flowerType[20];

        fgets(line, 1024, fpDataFile);

        sscanf(line, "%f,%f,%f,%f,%[^\n]",
               &feat[1][index], &feat[2][index],
               &feat[3][index], &feat[4][index], flowerType);
        printf("%d. %f, %f, %f, %f, %s\n", ((int)index + 1),
               feat[1][index], feat[2][index],
               feat[3][index], feat[4][index], flowerType);
        index++;
    }
    fclose(fpDataFile);
}

int checkObsnsSize(char *dataFileName) {
    int obsnsSize = 0;
    char line[1024];

    FILE *fpDataFile = fopen(dataFileName, "r");
    if (!fpDataFile) {
        printf("Missing input file: %s\n", dataFileName);
        exit(1);
    }
    while (!feof(fpDataFile)) {
        fgets(line, 1024, fpDataFile);
        obsnsSize++;
    }
    fclose(fpDataFile);
    return obsnsSize;
}
sscanf(line, "%f,%f,%f,%f,%[^\n]", &feat[1][index], &feat[2][index],
         &feat[3][index], &feat[4][index], flowerType);
printf("%d. %f, %f, %f, %f, %s\n", ((int) index+1), feat[1][index], feat[2][index],
         feat[3][index], feat[4][index], flowerType);

在這兩行中,您可以在這里訪問 index 越界&feat[4][index] 這會導致未定義的行為

由於數組的聲明是

 float feat[featSize][obsnsSize];     //where featSize is 4 

因此,您可以訪問從03而不是4索引數組索引從0開始)。

幾件事:

  • 不要檢查feof ,而是檢查返回值fgets()

     while (!feof(fpDataFile)) {
  • 始終檢查sscanf()的返回值。

  • 您的索引應該從 0 而不是 1 開始(索引 4 超出范圍):

     sscanf(line, "%f,%f,%f,%f,%[^\\n]", &feat[0][index], &feat[1][index], &feat[2][index], &feat[3][index], flowerType); printf("%d. %f, %f, %f, %f, %s\\n", ((int)index + 1), feat[0][index], feat[1][index], feat[2][index], feat[3][index], flowerType);
  • 正如@chqrlie 所述:使用%19[^\\n]避免溢出,因為flowerType大小僅為20

     sscanf(line, "%f,%f,%f,%f,%19[^\\n]", &feat[0][index], &feat[1][index], &feat[2][index], &feat[3][index], flowerType);

因此,將它們放在一起,正確的代碼如下所示:

1- 使用 fgets() 返回值來確定文件何時被完全讀取。
2- 從索引 0 開始讀入數組
3- 檢查 sscanf() 的返回值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int checkObsnsSize(char * dataFileName);
void readIris ();

int main () {
    readIris();
    return 0;
}

void readIris() {

    int featSize = 4;

    char *dataFileName = "iris.data";
    int obsnsSize = checkObsnsSize(dataFileName);
    float feat[featSize][obsnsSize];
    int label[obsnsSize];
    memset(feat, 0, featSize*obsnsSize*sizeof(float));
    memset(label, 0, obsnsSize*sizeof(int));

    printf("Obsns size is %d and feat size is %d.\n", obsnsSize, featSize);

    FILE *fpDataFile = fopen(dataFileName,"r");

    if (!fpDataFile) {
        printf("Missing input file: %s\n", dataFileName);
        exit(1);
    }

    int index = 0;
    char line[1024]; char flowerType[20];

    while (fgets(line, 1024, fpDataFile))
    {
        if( 5 == sscanf(line, "%f,%f,%f,%f,%19[^\n]", &feat[0][index], &feat[1][index], &feat[2][index], &feat[3][index], flowerType))
        {
            printf("%d. %f, %f, %f, %f, %s\n", ((int) index+1), feat[0][index], feat[1][index], feat[2][index], feat[3][index], flowerType);
            index++;
        }
    }
    fclose(fpDataFile);
}

int checkObsnsSize(char * dataFileName) {

    int obsnsSize = 0;
    char line[1024];

    FILE *fpDataFile = fopen(dataFileName,"r");
    if (!fpDataFile) {
            printf("Missing input file: %s\n", dataFileName);
            exit(1);
        }
    while (!feof(fpDataFile)) {
        fgets(line, 1024, fpDataFile);
        obsnsSize++;
    }
    fclose(fpDataFile);
    return obsnsSize;
}

暫無
暫無

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

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