'Fscanf Doesn't Take Inputs properly
I'm trying to take inputs with fscanf
and to give output to another file but fscanf
doesn't give the input it should give.
Here is my code:
#include <stdio.h>
int main() {
FILE *a;
FILE *b;
int i;
int played_matches, points, goaldif, ranking;
int wins, draws, losses, goals_scored, goals_against;
char team[18];
a = fopen("source.docx", "r");
b = fopen("aim.doc", "w");
for (i = 0; i < 10; i++) {
fscanf(a, "%d %s %d %d %d %d %d", &ranking, &team, &wins, &draws, &losses, &goals_scored, &goals_against);
played_matches = wins + draws + losses;
points = (3 * wins) + draws;
goaldif = goals_scored - goals_against;
fprintf(b, "%d %s %d %d %d ", ranking, team, played_matches, points, goaldif);
}
fclose(a);
fclose(b);
return 0;
}
Solution 1:[1]
#include <stdio.h>
int main() {
FILE *a;
FILE *b;
int i;
int played_matches, points, goaldif, ranking;
int wins, draws, losses, goals_scored, goals_against;
char team[18];
a = fopen("source.txt", "r");
b = fopen("aim.txt", "w");
for (i = 0; i < 10; i++) {
fscanf(a, "%d %s %d %d %d %d %d", &ranking, team, &wins, &draws, &losses, &goals_scored, &goals_against);
played_matches = wins + draws + losses;
points = (3 * wins) + draws;
goaldif = goals_scored - goals_against;
fprintf(b, "%d %s %d %d %d \n", ranking, team, played_matches, points, goaldif);
}
fclose(a);
fclose(b);
return 0;
}
& is for integer values.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user438383 |