我有两个文件,都有 47k 行。我正在尝试同时读取每个文件的行。问题是只读取每个文件的第一行。这就是我写的代码:
id_region = fopen(argv[3], "r");
prediction=fopen(argv[4], "r");
int prediction_class, class, region;
while ((fscanf(id_region,"%d 1:%d",&class,®ion) == 2) && (fscanf(prediction,"%d",&prediction_class) == 1))
{
fprintf(stderr,"\nRegião %d",region);
fprintf(stderr,"\nClasse %d",class);
fprintf(stderr,"\nPredição %d",prediction);
}
我的代码有什么问题?
编辑:我尝试了这段代码,但是当我运行它时出现分段错误。这里有什么问题?
int main(int argc, char** argv)
{
FILE* id_region;
FILE* prediction;
id_region = fopen(argv[1], "r");
prediction=fopen(argv[2], "r");
char line[50];
char line2[50];
int prediction_class,region,temp1,temp2;
while((fgets (line,10,prediction) != NULL) && (fgets (line2, 10, id_region)!=NULL))
{
//formating the input
temp1=fscanf(line,"%d",&prediction_class);
temp2=fscanf(line2,"%d",®ion);
fprintf(stderr,"\nRegion: %d",region);
fprintf(stderr,"\nPrediction: %d",prediction_class);
}
fclose(prediction);
fflush(prediction);
fclose(id_region);
fflush(id_region);
return(0);
}
通过此代码解决 - 谢谢!
int main(int argc, char** argv)
{
FILE* id_region;
FILE* prediction;
id_region = fopen(argv[1], "r");
prediction=fopen(argv[2], "r");
char line[50];
char line2[50];
int prediction_class,region,class;
contador=0;
while((fgets(line,10,prediction)!= NULL) && (fgets(line2, 20, id_region)!=NULL))
{
sscanf (line,"%d", &prediction_class);
sscanf (line2,"%d 1:%d",&class,®ion);
fprintf(stderr,"\nRegion: %d",region);
fprintf(stderr,"\nPrediction: %d",prediction_class);
}
fclose(prediction);
fflush(prediction);
fclose(id_region);
fflush(id_region);
return(0);
}