我试图从文件中读取一些值并将其存储在结构中,但是我不明白如何像数组一样存储它们,因此,如果我需要第一行中的第一个值,则可以提取它。另外我也不知道我在做什么错,它会在结构的同一变量中打印最后三个值,然后再次打印它们。 如果这是基本的C语言,很抱歉,但是我没有完全理解这些讲座,因此我真的不知道该怎么办,在此先非常感谢。 这是代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc!=3){
fprintf(stderr,"ERROR: 2 arguments required");
exit(EXIT_FAILURE);
}
//first we put all the data from the file into a struct
//then we create a variable to store the distance and the speed
//then we create another variable to store first and last check
//then we make a switch and we print the informations
FILE *input=fopen(argv[1],"r");
if(input==NULL){
fprintf(stderr,"ERROR: couldn't open the file");
exit(EXIT_FAILURE);
}
char a=getc(input);
int n=0;
while((a = getc(input))!=EOF){
if(a=='\n'){
n++;
}
}
char ch = getc(input);
struct BBS {
char* BUS_ID[5];
char* BUS_ROUTE[3];
char* BUS_X[6];
char* BUS_Y[6];
char* BUS_TIME[6];
};
struct BBS BUS[n];
for(int i=0;i<n;i++){
while(!feof(input)){
fscanf(input,"%s %s %s %s %s\n",&BUS[i].BUS_ID,&BUS.BUS_ROUTE[i],&BUS.BUS_X[i],&BUS.BUS_Y[i],&BUS.BUS_TIME[i]);
printf("ID: %s\n Route: %s\n X: %s\n Y: %s\n Time: %s\n",&BUS.BUS_ID[i],&BUS.BUS_ROUTE[i],&BUS.BUS_X[i],&BUS.BUS_Y[i],&BUS.BUS_TIME[i]);
printf("\n");
}}
fclose(input);
return 0;
}