Reputation: 131
I've been trying to figure out how to read the scores and storing them in array. been trying for sometime and its clearly not working out for me. Please help.
//ID scores1 and 2
2000 62 40
3199 92 97
4012 75 65
6547 89 81
1017 95 95//.txtfile
int readresults (FILE* results , int* studID , int* score1 , int* score2);
{
// Local Declarations
int *studID[];
int *score1[];
int *score2[];
// Statements
check = fscanf(results , "%d%d%d",*studID[],score1[],score2[]);
if (check == EOF)
return 0;
else if (check !=3)
{
printf("\aError reading data\n");
return 0;
} // if
else
return 1;
Upvotes: 3
Views: 4857
Reputation: 198324
You declare variables twice, once in parameter list and once in "local declarations".
The function brace is not closed.
One fscanf
can only read a number of items specified by its format string, in this case 3 ("%d%d%d"
). It reads numbers, not arrays. To fill an array, you need a loop (while
, or for
).
EDIT
Okay, here's one way to do it:
#define MAX 50
#include <stdio.h>
int readresults(FILE *results, int *studID, int *score1, int *score2) {
int i, items;
for (i = 0;
i < MAX && (items = fscanf(results, "%d%d%d", studID + i, score1 + i, score2 + i)) != EOF;
i++) {
if (items != 3) {
fprintf(stderr, "Error reading data\n");
return -1; // convention: non-0 is error
}
}
return 0; // convention: 0 is okay
}
int main() {
FILE *f = fopen("a.txt", "r");
int studID[MAX];
int score1[MAX];
int score2[MAX];
readresults(f, studID, score1, score2);
}
Upvotes: 2
Reputation: 3529
If you want to call that function just once and have it read the scores for all of the students, you should use something like this:
int i=0;
check = fscanf(results , "%d %d %d",&id[i],&score1[i],&score2[i]);
while(check!=EOF){
i++;
check = fscanf(results , "%d %d %d",&id[i],&score1[i],&score2[i]);
}
Upvotes: 2