使用逗号分割文件,将值存储在二维数组中

时间:2017-01-25 05:33:32

标签: c

目前正致力于如何使用","拆分.csv文件。然后创建一个 用于存储Alphabet和数字的二维数组。按照目前的情况,下面的代码输出:" a ,,,,,,,,,,,,,,,,,,,"。另外,声明二维数组的适当数据类型是什么,因为值是Char和int?此外,我知道这是一个重复的问题,因为我没有发现以前的问题有用。一个简单的解释将是伟大的和赞赏,关于如何使用这个片段代码分割文件的解释将是完美的"%* [^,]"如果可能的话。提前致谢。

以下.csv文件的示例内容。

A,1
B,2
C,3 
.....

该计划:

char single;
/* char array[26][2];  I was thinking the 2-d array would be declared like that. */

while ((single = fgetc(fpointer)) != EOF)
{
    fscanf(fpointer,"%*[^,]");
    printf("%c",single);
}
fclose(fpointer);


    ............................................................
    edit code: With strtok() and fgetc()
    ............................................................

    //char single;
    char s[26] = ",";
    char *token;
    char str[100];


    while (fgets(str,100,fpointer))
    {
            while((token = strtok(NULL, s)) != NULL)
            {
                    printf(" %s\n", token);
            }
    }

    fclose(fpointer);

2 个答案:

答案 0 :(得分:0)

typedef struct
{
    char charVal;
    int intVal;
}SplitValue;

SplitValue result[50];
int count = 0;

FILE *myFile = NULL;
fopen_s(&myFile, "mycsvfile.csv", "r");
char single[100];
if (myFile != NULL)
{
    while (fgets(single, 100, myFile) != NULL)
    {
        // store the first char value
        result[count].charVal = single[0];

        // store the int value as string
        char intval[25];
        int i = 0;
        for (i = 2; single[i] != '\n'; ++i)
        {
            intval[i - 2] = single[i];
        }
        intval[i-2] = 0;

        // convert the string to int, either using atoi or sscanf
        result[count].intVal = atoi(intval);

        // get ready for the next item
        count++;
    }
    fclose(myFile);
}

if (count)
{
    for (int i = 0; i < count; ++i)
    {
        printf("Char value: %c and int value: %d\n", result[i].charVal, result[i].intVal);
    }
}

希望这有帮助!

答案 1 :(得分:0)

考虑来自DYZ和RoadRunner的评论,请尝试以下解决方案。希望它有所帮助。

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

typedef struct charIntPair {
    char alpha;
    int  value;
} charIntPair_t;

#define MAX_ALPHABET_LENGTH 26
charIntPair_t myAlphabet[MAX_ALPHABET_LENGTH];
int alphabetLength = 0;

int main() {

    FILE *fp = fopen("mycsvfile.csv","r");

    if (!fp)
        return 1;  // File could not be opened.

    char line[100];
    for (alphabetLength=0; alphabetLength < MAX_ALPHABET_LENGTH && fgets(line,100,fp); alphabetLength++) {
        int elementsRead = sscanf (line,"%c,%d",
                    &myAlphabet[alphabetLength].alpha,
                    &myAlphabet[alphabetLength].value);
        if (elementsRead < 2) // not a valid char/int-combination?
            break;
    }

    for (int i=0; i<alphabetLength; i++) {
        printf("element %d is (%c,%d)\n", i, myAlphabet[i].alpha, myAlphabet[i].value);
    }

    return 0;
}

它希望字符是一行中的第一个元素,后面紧跟一个,。该号码可能预先有空格。以下输入产生以下输出:

A,1
B,2
C, 3
D,15
E,17

=>

element 0 is (A,1)
element 1 is (B,2)
element 2 is (C,3)
element 3 is (D,15)
element 4 is (E,17)