Reputation: 376
the link above contain the code that i wrote for a class project.I have a question about the output file for this code.The output file was created but was empty why is this happen ? I don't see anything wrong with the output function.Could it be that I did something wrong in the other functions? thank you here is the data for the code
LYSACEK Evan
1 7.5 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
2 10.0 1 2 2 1 1 1 1 2 0 1 1 1
3 3.0 1 2 2 2 1 0 1 2 1 1 1 2
4 3.1 1 1 1 1 1 0 0 0 1 0 1 0
5 1.7-3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
6 2.1 0 0 1 1 1 1 1 1 1 1 1 1
7 3.1 0 0 1 1 0 0 1 1 1 1 1 1
8 3.5 0 1 1 2 1 1 1 1 1 1 0 1
WEIR Johnny
1 7.5 2 2 2 2 1 1 1 1 1 2 1 1
2 10.0 1 1 1 1 2 0 1 1 1 1 2 1
3 3.0 1 1 1 2 1 0 1 1 2 2 2 2
4 3.1 1 2 1 2 1 1 0 0 2 1 1 0
5 5.5 0 -1 0 -1 -1 0 -1 -1 1 -2 -2 -2
6 1.3 1 1 1 2 1 1 1 0 1 1 1 2
7 3.1 0 1 1 1 1 0 0 1 2 1 1 1
8 3.0 -1 1 1 2 1 0 1 0 2 1 -1 1
PLUSHENKO Evgeni
1 13.0 0 2 1 1 1 0 1 0 1 1 1 1
2 7.5 1 2 2 2 2 1 2 1 2 2 2 2
3 6.0 2 1 1 1 1 0 0 2 1 2 1 2
4 2.3 2 1 1 1 1 1 2 1 1 1 1 1
5 3.4 2 2 2 2 1 2 3 3 2 3 2 1
6 2.1 1 1 1 2 2 0 0 0 1 2 1 1
7 3.1 1 0 2 2 1 1 1 2 2 2 2 1
8 3.5 1 1 2 2 1 1 1 1 2 2 1 1
SAVOIE Matthew
1 3.0 0 0 0 1 0 0 0 0 0 0 0 -1
2 7.5 1 2 2 1 1 1 1 1 1 1 2 2
3 9.5 0 1 1 0 0 0 0 0 0 0 1 1
4 3.1 1 1 1 1 1 1 0 0 1 1 0 0
5 1.9 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3
6 2.1 0 0 1 0 1 0 0 1 1 1 1 1
7 3.1 0 0 1 0 0 0 1 1 2 0 2 1
8 3.0 0 0 1 1 1 0 1 1 1 1 1 1
Upvotes: 1
Views: 104
Reputation: 11395
I am guessing your problem stems from the misunderstanding of working of sscanf
. When you use sscanf
the buffer pointer which it is reading is not moved further i.e. multiply calls will read the same buffer the same way. In your function getData
the following piece of code:
for(k = 0; k < MAX_ELEM; k++)
{
if(fgets(buffer, sizeof(buffer)-1, fpIn) != NULL)
{
sscanf(buffer,"%d %f", &skater[i].elements, &skater[i].baseval[k]);
for(j = 0; j < SCORE; j++)
{
sscanf(buffer,"%d", &skater[i].score[k][j]);
}
}
}
the value read in skater[i].elements
is stored in all of skater[i].score[k][j]
values as the skater[i].elements
will be the first int
read (assuming that the call is successful). You could choose to read all the elements in a single sscanf
call something on these (inelegant) lines:
for (k = 0; k < MAX_ELEM; k++) {
if (fgets(buffer, sizeof(buffer) - 1, fpIn) != NULL)
{
sscanf(buffer, "%d %f %d %d %d %d %d %d %d %d %d %d %d %d",
&skater[i].elements,
&skater[i].baseval[k],
&skater[i].score[k][0],
&skater[i].score[k][1],
&skater[i].score[k][2],
&skater[i].score[k][3],
&skater[i].score[k][4],
&skater[i].score[k][5],
&skater[i].score[k][6],
&skater[i].score[k][7],
&skater[i].score[k][8],
&skater[i].score[k][9],
&skater[i].score[k][10],
&skater[i].score[k][11]
);
}
}
Or you could also look into fscanf
for reading from file.
Side note: In printData
you should exit if fopen
fails. And you can use perror
for more meaning error messages when fopen
fails.
Hope this helps!
Upvotes: 1
Reputation: 43504
You need to close fpOut
file handle using fclose
. Otherwise the content might not be written to disk before your program finishes.
Hints:
"output.txt"
fpOut = stdout;
just to check that the output is printed on the console.On my system, running your code with your input.
NAME: LAB#6 EMAIL:
EVENT: MEN SHORT PROGRAM
CHIEF ACCOUNTANT:
SKATER: LUSHENKO Evgeni
[BASE] [SCORE] [TOTAL]
---------------------------------------------------------------
13.00 1 1 1 1 1 1 1 1 1 1 1 1 10.17
7.50 2 2 2 2 2 2 2 2 2 2 2 2 10.17
6.00 3 3 3 3 3 3 3 3 3 3 3 3 10.17
2.30 4 4 4 4 4 4 4 4 4 4 4 4 10.17
3.40 5 5 5 5 5 5 5 5 5 5 5 5 10.17
2.10 6 6 6 6 6 6 6 6 6 6 6 6 10.17
3.10 7 7 7 7 7 7 7 7 7 7 7 7 10.17
3.50 8 8 8 8 8 8 8 8 8 8 8 8 10.17
---------------------------------------------------------------
Total Base: 40.90 Total Technical Score: 63.82
SKATER: EIR Johnny
[BASE] [SCORE] [TOTAL]
---------------------------------------------------------------
7.50 1 1 1 1 1 1 1 1 1 1 1 1 6.33
10.00 2 2 2 2 2 2 2 2 2 2 2 2 6.33
3.00 3 3 3 3 3 3 3 3 3 3 3 3 6.33
3.10 4 4 4 4 4 4 4 4 4 4 4 4 6.33
5.50 5 5 5 5 5 5 5 5 5 5 5 5 6.33
1.30 6 6 6 6 6 6 6 6 6 6 6 6 6.33
3.10 7 7 7 7 7 7 7 7 7 7 7 7 6.33
3.00 8 8 8 8 8 8 8 8 8 8 8 8 6.33
---------------------------------------------------------------
Total Base: 36.50 Total Technical Score: 54.83
SKATER: YSACEK Evan
[BASE] [SCORE] [TOTAL]
---------------------------------------------------------------
7.50 1 1 1 1 1 1 1 1 1 1 1 1 10.17
10.00 2 2 2 2 2 2 2 2 2 2 2 2 10.17
3.00 3 3 3 3 3 3 3 3 3 3 3 3 10.17
3.10 4 4 4 4 4 4 4 4 4 4 4 4 10.17
1.70 5 5 5 5 5 5 5 5 5 5 5 5 10.17
2.10 6 6 6 6 6 6 6 6 6 6 6 6 10.17
3.10 7 7 7 7 7 7 7 7 7 7 7 7 10.17
3.50 8 8 8 8 8 8 8 8 8 8 8 8 10.17
---------------------------------------------------------------
Total Base: 34.00 Total Technical Score: 53.58
SKATER: AVOIE Matthew
[BASE] [SCORE] [TOTAL]
---------------------------------------------------------------
3.00 1 1 1 1 1 1 1 1 1 1 1 1 6.33
7.50 2 2 2 2 2 2 2 2 2 2 2 2 6.33
9.50 3 3 3 3 3 3 3 3 3 3 3 3 6.33
3.10 4 4 4 4 4 4 4 4 4 4 4 4 6.33
1.90 5 5 5 5 5 5 5 5 5 5 5 5 6.33
2.10 6 6 6 6 6 6 6 6 6 6 6 6 6.33
3.10 7 7 7 7 7 7 7 7 7 7 7 7 6.33
3.00 8 8 8 8 8 8 8 8 8 8 8 8 6.33
---------------------------------------------------------------
Total Base: 33.20 Total Technical Score: 50.28
Upvotes: 1