gpt4 book ai didi

c - 为什么我不能从数组中读取数据?

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:53 24 4
gpt4 key购买 nike

我正在从文件中读取数据并将其写入数组。这有效,但我似乎无法正确地从数组中读回它。哪里错了?

int main(){

struct mountain{
char name[300];
char height[5];
};

struct mountain mountainArray[8];

fp = fopen("berge.txt", "r");

if(fp == NULL) {
perror("Error opening file");
return(-1);
}

int i=0;
while ( fgets(readLine, buflen, fp)){
if(i<8){

char * p;

p = strtok (readLine,":");
if (p != NULL){

strcpy(mountainArray[i].name,p);
p = strtok (NULL, ":");
if (p != NULL)
strcpy(mountainArray[i].height,p);
}

i++;

}
}

unsigned int f;
for (f=0; f<8; f++){

printf("%s\n", mountainArray[i].name);
}
fclose(fp);

return 0;
}

最佳答案

排队

printf("%c\n", mountainArray[i].name);

我想你的意思是 f 作为索引,而不是 i

(Ivaylo 是对的 - 您需要 %s 作为字符串。)

关于c - 为什么我不能从数组中读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20048948/

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