gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:35:01 25 4
gpt4 key购买 nike

我对C语言比较陌生。我无法解决我的问题。我有一个输入文件,比方说 input.txt。我们知道每行有 4 列。然而,我们不知道有多少条线。我给你示例 input.txt:

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

第一列可以是 2 个不同的对象,例如学生或教师。第二列将是唯一的。没有重复的名字。第三列是该人的年龄。第四列是权重。另外,我正在尝试制作二维数组。所以,我的目标是:

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

我正在尝试创建这样的数组。该数组必须是动态的。因为我们不知道有多少行。我无法用逗号分隔每一行。我已经尝试过strdot 。如何用逗号解析行,并将它们添加到二维数组中?另外,我对指针感到困惑。创建二维数组时,我们是否必须使用 char **arrPtr; ?或者使用像 *arrPtr足以创建二维数组吗?

#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;
}

在 else 语句之后我无法继续。你能帮我吗?

最佳答案

例如,您的代码中有很多这样的错误。

while ( (c == fgetc(FileName)) != EOF ) { -> 常量 -1 与 bool 表达式的比较始终为 true

所以让我们重新开始吧。这就是我读取逗号分隔文件并将内存动态分配给“读取”对象数组的方式。实际上,它的代码行数比您预期的要少。

#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;
}

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

/安德斯

编辑

今天晚上有点无聊,所以让我们删掉代码吧……嗯,好吧。这不是我官方答案的一部分:-),但你可以像这样使用它(它实际上也跳过一层内存复制:)并且如果你将一行 C 代码分隔符定义为 ' ,则引擎是 3 行 C 代码;'->

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;
}

关于创建输入文件大小未知的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49098170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com