[英]text file into a dynamic array of structures C programing
I have a text file with this information 我有一个包含此信息的文本文件
AreaA 500 2
AreaB 200 1
AreaC 100 2
AreaD 1000 1
and I want to put read this information into a dynamic array of structures I made this struct 我想把这些信息读入一个动态的结构数组中,我制作了这个结构
typedef struct dadosZoo zoo, *pZoo;
struct dadosZoo {
char AreaID[ID_TAM];
int capacidade;
int adjacentes;
//areas adjacentes
};
and then i created a function 'area_Estrutura' which was supposed to read the file and put the information into a dynamic array of structures. 然后我创建了一个函数“ area_Estrutura”,该函数应该读取文件并将信息放入动态的结构数组中。
void area_Estrutura(char *nomeFich, zoo *z){
FILE *f;
/* int cap, adj;
char areId;*/
//zoo *z;
f = fopen(nomeFich, "r");
if (f ==NULL){
printf("Erro na abertura do ficheiro %s para leitura.\n", nomeFich);
return;
}
while(fscanf(f, "%c\t%d\t%d\n", &z->AreaID, &z->capacidade, &z->adjacentes)!=EOF){
z = malloc(sizeof(zoo));
if(z!=NULL){
fscanf(f, "%c\t%d\t%d\n", &z->AreaID, &z->capacidade, &z->adjacentes);
fprintf(f, "%s\t%d\t%d\n", z->AreaID, z->capacidade, z->adjacentes);
free(z);
}
}
fclose(f);
}
I never know how many structures but one line is one structure so I did this 我不知道有多少种结构,但一条线就是一种结构,所以我做到了
while(fscanf(f, "%c\t%d\t%d\n", &z->AreaID, &z->capacidade, &z->adjacentes)!=EOF)
that reads the file while not end of the file in this line i don't know if i should read it like that or with the variables that i created up there 在该行中读取文件而不是文件末尾的时候,我不知道是否应该像那样读取它或使用我在那里创建的变量读取它
char areId;
int cap, adj;
then in the main i put this call to function 然后在主要我把这个调用功能
zoo *zoo;
area_Estrutura("Zoo.txt", zoo);
and i wanted it to print on the screen. 我希望它能在屏幕上打印。 when i build the code i don't have any mistakes or warnings but it doesn't run.
当我构建代码时,我没有任何错误或警告,但没有运行。 Does someone have any idea.
有人有什么主意吗? Sorry if the formatation or the english is a little bit confusing but it is not my mother toungue, thank you.
抱歉,如果格式或英语有点混乱,但这不是我妈妈的舌头,谢谢。
You need to change fscanf(f, "%c\\t%d\\t%d\\n", &z->AreaID, &z->capacidade, &z->adjacentes) to fscanf(f, "%s\\t%d\\t%d\\n", z->AreaID, &z->capacidade, &z->adjacentes) 您需要将fscanf(f,“%c \\ t%d \\ t%d \\ n”,&z-> AreaID,&z-> capacidade,&z-> adjacentes更改为fscanf(f,“%s \\ t%d \\ t%d \\ n“,z-> AreaID,&z-> capacidade和&z-> adjacentes)
ie %c to %s and do not pass address of areadID 即%c到%s,并且不传递areadID的地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.