Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将CSV字符串导入C中的多维数组_C_Arrays_Csv - Fatal编程技术网

将CSV字符串导入C中的多维数组

将CSV字符串导入C中的多维数组,c,arrays,csv,C,Arrays,Csv,我试图以类似于CSV的格式将文本文件输入多维数组,其中数组的每个元素都是每行的单词数组。任何帮助都将不胜感激 例如,文件input.txt可能包含: Carrot, Potato, Beetroot, Courgette, Broccoli Dad's oranges, Apple, Banana, Cherry Pasta, Pizza, Bread, Butter 我希望从中得到的输出阵列的结构如下: [[Carrot, Potato, Beetroot, Courgette, Brocc

我试图以类似于CSV的格式将文本文件输入多维数组,其中数组的每个元素都是每行的单词数组。任何帮助都将不胜感激

例如,文件input.txt可能包含:

Carrot, Potato, Beetroot, Courgette, Broccoli
Dad's oranges, Apple, Banana, Cherry
Pasta, Pizza, Bread, Butter
我希望从中得到的输出阵列的结构如下:

[[Carrot, Potato, Beetroot, Courgette, Broccoli], [Dad's oranges, Apple, Banana, Cherry], [Pasta, Pizza, Bread, Butter]]
因此,您可以使用该行:

printf("%s", inputArray[1][0]);
将打印:

Dad's oranges

我不确定这里的问题是什么。但是,看看您的问题陈述和代码,我发现问题很少(注意,我没有运行代码,这是为了给您一个想法):

  • 您的varCount将从1开始,在放入第一个单词之前递增
  • 您正在将一个固有的二维数据存储到一个一维数组中。这通常很好,但您需要对行的起始位置和结束位置进行编码。这是缺失的。如果一切正常,您将得到一个单词数组,而不知道行的开始/结束位置。处理此问题的一种方法是创建二维阵列。另一种方法是在两行之间插入一个指向已知单词的指针。下面是显示插入分隔符的代码段

    char *knownWord = "anyword";
    ...
    while (fgets(line, maxLineLength, inputFile))
    {
        token = strtok(&line[0], ",");
        while (token) {
            inputArray[varCount] = token;
            varCount++;
            token = strtok(NULL, ",");
        }
        inputArray[varCount] = knownWord;
        varCount ++;
    }
    
  • 为此,打印将以如下方式进行

        bool atKnownWord = 0;
        printf("[");
        for (i = 0; i < maxWords; i++) {
                if (inputArray[i] == NULL) {
                        break;
                }
                if (inputArray[i] == knownWord) {
                        atKnownWord = 1;
                        printf("]");
                        continue;
                }
                if (atKnownWord) {
                        atKnownWord = 0;
                        printf(", [");
                }
                printf("%s", inputArray[i]);
        }
        printf("]");
    
    bool-atknowrd=0;
    printf(“[”);
    对于(i=0;i
    您尚未分配内存来存储
    令牌

    此外,在数组中存储
    令牌之后,还应增加
    varCount

    代码可以写成:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define maxLineLength 1000  //Maximum length of a line
    #define wordsPerLine 200    //Maximum words in a line
    #define maxLines 200        //Maximum lines in an input
    
    int main(int argc, char *argv[])
    {
        char line[maxLineLength] = {0};
        char *inputArray[maxLines][wordsPerLine] = {};
        char *ptr, *token;
        int i, j, lines, maxWords = 0;
    
        FILE *inputFile = fopen("input.txt", "r");
    
        if (inputFile)
        {
            i = j = 0;
            while (fgets(line, maxLineLength, inputFile))
            {
                token = strtok(&line[0], ",\n");
                while(token)
                {
                    if(ptr = malloc(sizeof(char) * (strlen(token)+1))) //whether malloc succeeded
                    {
                        if(token[0] == ' ')
                            strcpy(ptr, token+1);
                        else
                            strcpy(ptr, token);
    
                        inputArray[i][j++] = ptr;
                        token = strtok(NULL, ",\n");
                    }
                    else
                    {
                        printf("malloc failed!\n");
                        exit(1);
                    }
                }
                if(maxWords < j)
                    maxWords = j;
                i++;
                j = 0;          
            } 
            lines = i;
            fclose(inputFile);
    
            for(i = 0; i < lines; i++)
            {
                for(j = 0; (j < maxWords) && inputArray[i][j]; j++)
                    printf("%s | ", inputArray[i][j]);
                printf("\n");
            }
        }
        return 0;
    }  
    
    #包括
    #包括
    #包括
    #定义maxLineLength 1000//线的最大长度
    #定义单词sperline 200//一行中的最大单词数
    #定义maxLines 200//输入中的最大行数
    int main(int argc,char*argv[])
    {
    字符行[maxLineLength]={0};
    字符*输入阵列[maxLines][wordsPerLine]={};
    字符*ptr,*令牌;
    int i,j,行,maxWords=0;
    FILE*inputFile=fopen(“input.txt”、“r”);
    如果(输入文件)
    {
    i=j=0;
    while(fgets(行、maxLineLength、inputFile))
    {
    令牌=strtok(&line[0],“,\n”);
    while(令牌)
    {
    if(ptr=malloc(sizeof(char)*(strlen(token)+1))//malloc是否成功
    {
    如果(令牌[0]='')
    strcpy(ptr,令牌+1);
    其他的
    strcpy(ptr,token);
    输入阵列[i][j++]=ptr;
    令牌=strtok(空,“,\n”);
    }
    其他的
    {
    printf(“malloc失败!\n”);
    出口(1);
    }
    }
    if(maxWords
    我建议strtok
    的分隔符字符串应该是
    ,\t\r\n“
    ,但是“爸爸的橙子”包含一个空格。因此,您必须在分隔符中保留该空格,并在获取每个标记后去掉所有前导空格。您正在观察什么输出?你期望得到什么样的输出?@rootkea目前我观察到奇怪的输出,我无法解释。对于
    printf(“%s”,inputArray[0])我回来了
    意大利面
    。我期待第一个项目,
    胡萝卜
    @hardanger是的!这是因为您没有分配内存来存储以前的令牌。但是由于
    输入阵列[0]
    指向
    行[0]
    它现在包含
    意大利面
    ,它覆盖了先前的
    胡萝卜
    爸爸的橙子
    查看我的答案以了解更多细节。您在
    ptr
    malloc
    之间使用了
    =/code>而不是
    。在我的回答中看到更新的代码。非常感谢@virtual real我在加入后更接近了,但仍然不太正确。(我不想打印带括号的数组表示,但您的代码仍然有帮助)。为了便于讨论,请按照您的建议打印出我得到的
    [PastaPizzareader
    ],[PastaBreadButter Cherry
    ],[PastaBreadButter Bread Butter]
    。有什么想法吗?@hardanger将“printf(“%s”,inputArray[i]);”替换为“printf(“%s”,inputArray[i]);”。请注意%s后面的空格,谢谢,但我认为这个问题更深了一点,我奇怪地变得高兴起来,例如输出在错误的行中,这就是我现在得到的:
    [意大利披萨阅读者
    -新行-
    ],[意大利面包奶油樱桃
    -新行-
    ],[意大利披萨面包奶油]]
    @hardanger您的问题是多方面的。1.您需要删除“\n”字符。检查。2.strtok()操作原始缓冲区,从而更改其原始输入。我建议您使用strcpy()或strdup()来复制“行”。当然,您需要释放新创建的缓冲区,但这是一个单独的主题。感谢@virtual real-尽管这种更改肯定不会影响更广泛的问题,即输出文本不正确,例如,正如我的问题中所述,输出应该开始
    [[胡萝卜、土豆、甜菜根,
    ,然后开始
    [意大利面食披萨阅读者
    。有什么想法吗?嗨@rootkea-非常感谢你的回答。我已经尝试实现了这一点,但只是得到了你