创建具有未知大小的输入文件的数组

时间:2018-03-04 17:25:37

标签: c multidimensional-array

我是C语言的新手。我无法解决我的问题。我有一个输入文件,让我们说 input.txt 。我们知道每行有4列。然而,我们不知道有多少行。我给你样本input.txt:

Student,James,12,65
Teacher,Jane,23,60
Teacher,Michael,30,75
Student,Erric,15,73

第一栏可以是2个不同的东西,如学生或老师。第二列将是唯一的。没有重复的名字。第3列将是该人的年龄。第四列将是权重。此外,我正在尝试制作2D阵列。所以,我的目标是:

arrName = {{Student, James, 12, 65}, {Teacher,Jane,23,60}, {Teacher,Michael,30,75}, {Student, Erric, 15,73}}

我正在尝试像这个数组一样创建。数组必须是动态的。因为我们不知道有多少行。我不能用逗号分割每一行。我试过了strdot。如何用逗号解析这些行,并将它们添加到2D数组中?另外,我对指针感到困惑。在创建2D数组时,我们是否必须像char **arrPtr;一样使用?或者使用类似*arrPtr就足以创建2D数组?

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

#define COLUMN 4        //We know that there are 4 column in chars.txt file.

int main(int argc, char const *argv[]) {
    char *arrName;
    int nameLines;

    arrName = (char *) malloc( sizeof( char ) );

    FILE *FileName;

    FileName = fopen(argv[1], "r");


    if (FileName == NULL) {
        printf("The Name file could not open!\n");
        exit(0);
    }

    else{
        char c;

        while ( (c == fgetc(FileName)) != EOF ) {
            if (c == '\n') {
                nameLines++;
            }
        }
        printf("%d\n", nameLines);
    }


    return 0;
}

在其他陈述后我无法继续。你能救我吗?

1 个答案:

答案 0 :(得分:0)

例如,你的代码中有很多错误。

Comparison of constant -1 with boolean expression is always true - &gt; #include <stdio.h> #include <stdlib.h> #include <string.h> #define COLUMNS 4 #define MAX_STRING_LENGTH 100 //Enter the maximum characters allowed as profession and name typedef struct theDAta{ char type[MAX_STRING_LENGTH+1]; char name[MAX_STRING_LENGTH+1]; unsigned int age; unsigned int weight; }theData; int main(int argc, char const *argv[]) { theData* allDataPtr=malloc(1); //Pointer to all entries theData currentData; //The current read data int currentBlock=0; //the index currently 'active' FILE *fp = fopen(argv[1], "r"); if (!fp) { printf("The file you provided can't be read!\n"); exit(EXIT_FAILURE); } while (fscanf(fp, "%[^,],%[^,],%d,%d\n", currentData.type, currentData.name,&currentData.age,&currentData.weight) == COLUMNS) { allDataPtr=realloc(allDataPtr, sizeof(theData)*(currentBlock+1)); if (!allDataPtr) {exit(EXIT_FAILURE);} //Memory allocation failure. Ok so here i lost my pointer to the previous memory.. However we let the OS garbage collect us. memcpy(allDataPtr+currentBlock++, &currentData, sizeof(currentData)); } for (int x=0; x<currentBlock; x++) { printf("Profession: %s\nName: %s\nAge: %d\nWeight: %d\n\n",allDataPtr[x].type,allDataPtr[x].name,allDataPtr[x].age,allDataPtr[x].weight); } fclose(fp); free(allDataPtr); return 0; }

让我们重新开始吧。这就是我如何读取逗号分隔文件并动态地将内存分配给'read'对象的数组。它实际上比你期望的更少的代码行。

typedef struct j{
    char t[MAX_STRING_LENGTH+1];
    char n[MAX_STRING_LENGTH+1];
    unsigned int a;
    unsigned int w;
}j;

int main(int argc, char const *argv[]) {
    j* g;
    int l=0;
    FILE *h;
    if((!(h=fopen(argv[1],"r"))||(!(g=malloc(sizeof(j))))))exit(-1);
    while(fscanf(h,"%[^,],%[^,],%d,%d\n",g[l].t,g[l].n,&g[l].a,&g[l].w)==COLUMNS)if(!(g=realloc(g,sizeof(j)*(++l+1))))exit(-1);
    for(int x=0;x<l;x++)printf("Profession: %s\nName: %s\nAge: %d\nWeight: %d\n\n",g[x].t,g[x].n,g[x].a,g[x].w);
    fclose(h);
    free(g);
    return 0;
}

所以我要做的是创建一个包含我想从文件中填充的东西的结构。然后我填充该对象并使用该对象的大小扩展内存,然后将读取的数据复制到该内存块的末尾。这基本上就是...希望你获得好成绩:) !!

/安德斯

修改

今天晚上有点无聊,所以让我们刮胡子代码......哼哼,好吧。这不是我官方答案的一部分:-)但你可以这样做(它实际上也是跳过一层内存副本:)如果你定义一行C代码定界符,引擎是3行C代码;' - &GT;

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition>
    <pa:panelTree styleClass="corps-grade-list"
        treeContent="#{affectationController.getDossierEnBrefAffectations()}"
        rendered="#{agentModele.estMigre}" />
</ui:composition>
</html>