Okay, so i've tried the fprintf and fscanf out a little and printing to the scoreboard textfile works, and reading from it works halfway... I can read the entries on the scoreboard, but it reads one step to far and fills the last entry, that doesnt actually exist, with garbage...
It doesnt sort the entries either, i tried the bubblesort and i got it to sort the points just like i want, but the names didnt follow... I must have changed something, though, cause its not working anymore >.<
I made a function called showScoreboard to read and show the entries...
Code:
void showScoreboard(Player player[])
{
FILE *scoreboardfile = fopen("scoreboard.txt", "r");
int i;
/*system("CLS");*/
for(i=0; i<MAX && !feof(scoreboardfile); i++)
{
fscanf(scoreboardfile, "%s %d", player[i].name, &player[i].points);
printf("name: %s, points: %d\n\n", player[i].name, player[i].points);
}
printf("\n\n\n");
system("pause");
fclose(scoreboardfile);
}
I also made the function sortScoreboard...
Code:
void sortScoreboard(Spelare player[])
{
int i, j, tempPoints;
char tempName[20];
int howMany=0;
FILE *scoreboardfile = fopen("scoreboard.txt", "r");
FILE *scoreboardfile2 = fopen("scoreboard.txt", "w");
for(i=0; i<MAX && !feof(scoreboardfile); i++)
{
fscanf(scoreboardfile, "%s %d", player[i].name, &player[i].points);
/*printf("name: %s, points: %d\n\n", player[i].name, player[i].points);*/
howMany++;
}
printf("\n\n\nhowMany: %d\n\n\n",howMany);
for(i=0; i<howMany; i++)
{
for(j=0; j<howMany-1; j++)
{
if(player[i].points > player[i+1].points)
{
tempPoints = player[i].points;
/*tempName = player[i].name;*/
player[i].points = player[i+1].points;
/*player[i].name = player[i+1].name;*/
player[i+1].points = tempPoints;
/*player[i+1].name = tempName;*/
}
}
}
for(i=0; i<howMany; i++)
{
printf("%s %d\n", player[i].name, player[i].points);
fprintf(scoreboardfile2, "%s %d\n", player[i].name, player[i].points);
}
fclose(scoreboardfile);
fclose(scoreboardfile2);
}
I open the file with two file pointers, one for reading and one for writing, then its supposed to read the entries on the scoreboard, sort it with bubblesort, and then print them unto the scoreboard again, overwriting the old one... It doesnt completely work though.
I also changed the addToScoreboard function to make use of the struct Player...
Code:
void addToScoreboard(int numberOfGuesses)
{
FILE *scoreboardfile = fopen("scoreboard.txt", "a");
Player player;
printf("\n\nName: ");
scanf("%s", player.name);
player.points = numberOfGuesses;
fprintf(scoreboardfile, "%s %d\n", player.name, player.points);
fclose(scoreboardfile);
}
So, whatever help that can be given is very much appreciated!
Originally Posted by
Adak
When you're done with reading all the file data, you will sort the records, according to their scores, and display the top group.
I'll show you how to do that, using an index array. It makes it a lot easier.
One of the requirements is that the scoreboard must be sorted with bubblesort though, so im kind of restricted :S
EDIT: Hmm, i tried out the code again now and it's completely messed up now... :S
All the sortScoreboard function does is write garbage to the scoreboard!