1

The first line in the is as follows

Fre     18/07 18.30     FCN - FCV       3 - 2      3.349  

and the scanning function as:

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;
}

I expected to see the following to be put into the 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

However, as the title says, the function doesn't return 9, and when testing it returns -1

EDIT

Added the struct

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;
};

and added to the scan that i declare an array of the struct

Community
  • 1
  • 1

1 Answers1

2

You have put an semicolon directly after the if condition making the code run on error being an empty statement.

The compound statement that follows is executed no matter if there's an error or not.

To avoid this and other "stupid" errors you should enable compiler warnings as much as you can bear. For example on gcc this means adding -Wall -Wextra arguments to the compiler (you can check the documentation for other warnings to enable).

skyking
  • 13,817
  • 1
  • 35
  • 57
  • So i tried edditing it to `if(fscanf(file_pointer, "%c", &c) != 1){printf("Error\n");}` which should work right? since what is inside the scope is only run if the if statment is *true* – Dream_Reaper Nov 26 '15 at 14:35
  • It does not, i can't for the life of me figure out why my logic is: if -> fscanf != 1 print error, and the fscanf should always return 1 if 1 varaible is correctly assigned, which it does since the rest of the function works as intended – Dream_Reaper Nov 26 '15 at 14:47
  • You still doesn't seem to bother to check `errno`. But first of course you should check that the return value actually indicates an error happened (ie if it returns negative - that should be `EOF`) as opposed to not converting all values. When checking your code I actually assigned the return value to a variable to be printed. – skyking Nov 26 '15 at 14:56
  • Thing is, it's not an error, the code runs, it just doesn'trun as expected, i have figured out that it is because when the file hits EOF the last check will have `c = -1` since that is the representation for EOF, results in the line of code being run, thanks for helping though, i've leanrt something new about error handling today and can't wait to try it out in my current assignment :-) – Dream_Reaper Nov 26 '15 at 15:22