I'm new to C programming language and I'm having a really difficult time trying to do this:
I have a file .txt created by my program that store the leaderboard of the 2048 game that I'm doing, and I think that the easiest way to make the leaderboard is by appending the result to the end of the file and than doing a bubble sort, but I can't find the correct value (score) of the first place to compare with the next place, and so on. Here is my code:
FILE* ptr;
prt = fopen("Placar.txt", "a");
if(ptr == NULL)
{
printf("Error!");
}
fclose(ptr);
ptr = fopen("Placar.txt", "r");
int ch, lines = 0, aux;
char line[150];
while(!feof(ptr))
{
ch = fgetc(ptr);
if (ch == '\n')
{
lines++;
}
}
fclose(ptr);
ptr = fopen("Placar.txt", "a");
fprintf(ptr, "%d: Name: %s - Score: %d - Movements: %d\n", lines+1, name, score, movements);
fclose(ptr);
// bubble sort
ptr = fopen("Placar.txt", "r+");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
while (!feof(ptr))
{
fgets(line, 150, ptr);
if (strstr(linha, "Score: ") != NULL)
{
// dont know how to do
}
}
}
}
fclose(ptr);
// TODO: delete the 11th line to make the top 10 only
If you think that I'm overcomplicating this, let me know how would you do. Thanks for the help.