I have a file like this :
name1 nickname1
name2 nickname2
name3 nickname3
And I want my programm read that file and show the name/nicknames couples.
Here is what I did :
users_file = fopen("users", "r");
while(!feof(users_file))
{
fscanf(users_file, "%s %s", &user.username, &user.name);
printf("%s | %s\n", user.username, user.nickname);
}
And here is the output :
name1 | nickname1
name2 | nickname2
name3 | nickname3
name3 | nickname3
Why is the last one repeated ?
Thanks
You need to check
feof()immediately after thefscanf(), or, alternatively, check the return value fromfscanf()itself. The last one is repeated becausefscanf()does not read any new data intouser.usernameanduser.nicknamedue to eof being reached.Possible fixes:
or: