0

I have a highscore file where name and score are stored. Each entry is divided by ; and between name and score is an - Here is an example:

15-Player One;10-Player Two;20-Player Three;10-Player Four;8-Player Five

I am now reading the content of the file into a char * buffer

My goal is now to separate each entry from another [kinda like buffer.Split(';') for C#] and print the list in an ascending order.

Any pro tips on how to do it the easiest way? I am currently having a blackout...

  • http://ideone.com/1KuQ7b here is the code you are looking for – Rohith R Sep 01 '16 at 02:44
  • PRP this is exactly what I was looking for! Thanks alot. –  Sep 01 '16 at 02:50
  • Hey PRP. After testing your solution I found it works good so far but after the second time I run through the highscore process, parts of the struct name and score values get filled with incredibly high numbers, dots or random system data info like the path to Internet Explorer.... I have no clue what's going on but am assuming some sort of memory leak??? –  Sep 01 '16 at 11:46
  • Can u give me the code you are referring about ? See the char array inside the struct is 100 in size and the number of entries in the struct is only 10....pls take care of that....or u can just post the code – Rohith R Sep 01 '16 at 12:28
  • Sure thing, here is code [relevant part]: http://pastebin.com/UQPVQrc9 Thank you alot for your help btw! –  Sep 01 '16 at 12:56

1 Answers1

0

From string.h, you can use strtok() :

#include <string.h>
#include <stdio.h>

int main()
{
    char scores[256] = "15-Player One;10-Player Two;20-Player Three;10-Player Four;8-Player Five";
    const char separator[2] = ";";
    char *entry;

    // Get the first entry
    entry = strtok(scores, separator);

    // Get all the others
    while (NULL != entry)
    {
        printf("%s\n", entry);

        /* You can also use it here to separate the actual score from the player */

        entry = strtok(NULL, separator);
    }

    return 0;
}
totorigolo
  • 71
  • 1
  • 7
  • Thanks for your answer. I actually did it the way PRP suggested with his code example at ideone.com/1KuQ7b since it also included the sorting already. It works nice but I am facing a problem that I always had with this program: After the second time the highscore screen shows up, some strange values appear in the actual struct. Random numbers, dots, paths to system variables. They are not in the actual highscore file and I can't even debug it to the moment where it happens. I'm totally out of ideas what that is.... –  Sep 01 '16 at 11:59