第一行如下
Fre 18/07 18.30 FCN - FCV 3 - 2 3.349
和扫描功能:
struct match_data match[300];
while(!feof(file_pointer))
{
if(fscanf(file_pointer, "%[^ ] %d/%d %[^ ] %[^ ] - %[^ ] %d - %d %lf \n",
match[i].match_day, &(match[i].match_date_day), &(match[i].match_date_month),
match[i].match_time, match[i].match_home_team, match[i].match_away_team,
&(match[i].match_home_goals), &(match[i].match_away_goals), &(match[i].match_spectators)) != 9);
{
printf("Error Reading file");
return -1;
}
我希望看到以下内容放入struct
match_day:FRI
match_date_day:18
match_date_month:07
match_time:18.30
match_home_team:FCN
match_away_team:FCV
match_home_goals:3
match_away_goals:2
match_spectators:3.349
但是,如标题所示,该函数不返回9,并且在测试时返回-1
添加了结构
struct match_data
{
char match_day[4];
int match_date_day;
int match_date_month;
char match_time[6];
char match_home_team[4];
char match_away_team[4];
int match_home_goals;
int match_away_goals;
double match_spectators;
};
并添加到扫描中,我声明了一个struct
的数组答案 0 :(得分:2)
你在if
条件之后直接放了一个分号,使代码运行时出错是一个空语句。
无论是否存在错误,都会执行以下复合语句。
为了避免这种情况和其他愚蠢的"错误你应该尽可能多地启用编译器警告。例如,在gcc
上,这意味着向编译器添加-Wall -Wextra
个参数(您可以查看文档以获取其他警告以启用)。