R1 Fre 17/07/2015 18.00 FCN – SDR 0 – 2 3.211
我必须让程序将其读入一个结构,但是当我尝试打印信息时,它出错了.我的代码看起来像这样:
#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX_input 198typedef struct game{ char weekday[4],home_team[4],away_team[4]; int round,hour,minute,day,month,year,home_goals,away_goals,spectators;}game;game make_game(file *superliga);int main(voID){ int input_number,number_of_games = 198,i = 0; game tIEd[MAX_input]; file *superliga; superliga = fopen("superliga-2015-2016.txt","r"); for(i = 0; i < number_of_games; ++i){ tIEd[i] = make_game(superliga); printf("R%d %s %d/%d/%d %d.%d %s - %s %d - %d %d\n",tIEd[i].round,tIEd[i].weekday,tIEd[i].day,tIEd[i].month,tIEd[i].year,tIEd[i].hour,tIEd[i].minute,tIEd[i].home_team,tIEd[i].away_team,tIEd[i].home_goals,tIEd[i].away_goals,tIEd[i].spectators);} fclose(superliga); return 0;}game make_game(file *superliga){ double spect; struct game game_info; fscanf(superliga,"R%d %s %d/%d/%d %d.%d %s - %s %d - %d %lf\n",&game_info.round,game_info.weekday,&game_info.day,&game_info.month,&game_info.year,&game_info.hour,&game_info.minute,game_info.home_team,game_info.away_team,&game_info.home_goals,&game_info.away_goals,&spect); game_info.spectators = spect * 1000; return game_info; }解决方法 问题出在您的文件中.它以空格开头,而不是如控制字符串中所述的R.
检查fscanf()的返回值,你会发现它每次都为零.
如果在fscanf()调用中添加前导空格,问题就会解决,如下所示:
fscanf(superliga," R%d %s %d/%d/%d %d.%d %s - %s %d - %d %lf\n",&spect);总结
以上是内存溢出为你收集整理的不会从文件读取到结构全部内容,希望文章能够帮你解决不会从文件读取到结构所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)