0

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
bartche
  • 13
  • 1
  • C and C++ are different languages. In general, tag only the language you are writing / compiling unless asking about comparisons or translation type issues. – crashmstr Oct 22 '21 at 18:32
  • 1
    I think it would be very difficult to sort a file in-place. Read the data into an array and sort that. Personally, I would use something like sqlite to store the data. – 001 Oct 22 '21 at 18:33
  • 1
    Why append it to the end? Make sure, you always insert new results at the right place and you can avoid sorting the file alltogether – lulle2007200 Oct 22 '21 at 18:35
  • read all value from text file then store it in a array then you can apply Bubble sort. – Kayes Fahim Oct 22 '21 at 18:46
  • 1
    Note: [Why is `while(!feof(file))` always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – John Bollinger Oct 22 '21 at 18:58
  • `printf("Error!"); ` - Why not exit then? Why close the file and reopen it? – Ed Heal Oct 23 '21 at 07:01

1 Answers1

4

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

I don't think that's easiest at all. It's especially bad if, as appears to be the case, you have in mind to use the file as your workspace instead of reading the contents into memory, sorting there, and then writing the results back out.

If you think that I'm overcomplicating this, let me know how would you do.

Consider this alternative:

  • open the high-score file and a temporary file
  • read scores from the original file one by one and write them to the temp file, until you find the first that is less than the new score you are recording (or reach the end of the file)
  • write the new score to the temp file
  • copy the rest of the scores from the original file to the temp file
  • close both files and replace the original with the temp file.

That involves at most all the same reading, at most all the same writing, and no sorting except for just inserting the new score at the correct place in the sequence. If you like, you can consider it a variation on Insertion Sort. As a bonus, it also has the same, minimal, storage requirements no matter how many scores there are, and therefore does not require any dynamic memory allocation.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • I don't know why i didn't thing this before, it is actully a lot easier than what I was trying to do. Thanks for the reply. – bartche Oct 22 '21 at 19:24
  • 1
    @bartche - It nice to say thanks, but clicking the hollow check is better :). – ryyker Oct 22 '21 at 19:25