-1

i have this function and dont work, in ubuntu show me Segmentation fault (core dumped) when i call the function:

void loadSavedGame(char fileName[], struct saveGame *savedGamesReaded[], int *length, int *statusCode){

    char line[500];
    char *token2;
    int x=0;

    *savedGamesReaded = (struct saveGame*)malloc((*length)*sizeof(struct saveGame)); //reserve memory for pointer and array elements

    int j;
    for(j=0; j<*length; j++){
        (*savedGamesReaded)[j].id = (char *)malloc(MAX_STRING*sizeof(char)); 
        (*savedGamesReaded)[j].score = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].position = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].maze_level = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].achievements = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].time_playing = (char *)malloc(MAX_STRING*sizeof(char));
        (*savedGamesReaded)[j].virtual_players = (char *)malloc(MAX_STRING*sizeof(char));
    }


    while (feof(file) == 0){ //file its a text plane was opened before
        fgets(line,500,file);
        token2 = strtok(line,":");    // Here the program falls on the fourth loop 
        strcpy((*savedGamesReaded)[x].id, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].score, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].position, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].maze_level, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].achievements, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].time_playing, token2);
        token2 = strtok(NULL,":");
        strcpy((*savedGamesReaded)[x].virtual_players, token2);
        x++;
    }
    fclose(archivo);


}

I declared the struct so:

struct saveGame{
   char *id;
   char *score;
   char *position;
   char *maze_level;
   char *achievements;
   char *time_playing;
   char *virtual_players;
};

I think the strtok is not working, but i do not know why, maybe the NULL in the token that's wrong?

strcpy((*savedGamesReaded)[x].id, token2);
token2 = strtok(NULL,":");
  • 2
    You tagged C++ so why don't you just use `std::vector` and `std::string` so that these annoying memory issues simply disappear. – Neil Kirk Sep 15 '15 at 20:34
  • Read this: http://stackoverflow.com/q/5431941/3185968 – EOF Sep 15 '15 at 20:36
  • 2
    Use a debugger to find the problem yourself. Learn much more in the long run if you persevere on your own rather than turning to help so soon. For example, you are just guessing at where the seg fault is happening. So obviously haven't used a debugger or even tried with basic debug print statements. – kaylum Sep 15 '15 at 20:38
  • 1
    Pick a language! C and C++ are **different** languages! – too honest for this site Sep 15 '15 at 20:40
  • It really looks like you love making your life more complicated, but beside that it is strange to have the crash always at the fourth loop, could you try to see what is inside line[] at the fourth loop? – Marco Sep 15 '15 at 20:41
  • Hm, I wonder if `strtok` could ever return `NULL` and whether I should check for that or not? – kaylum Sep 15 '15 at 20:47
  • sorry guys, i edited the post, its only C – Profesora Ximena Sep 15 '15 at 20:48

2 Answers2

0

This line doesn't do what you think it does:

while (feof(file) == 0){ //file its a text plane was opened before

feof() only returns true after EOF is reached. See Why is “while ( !feof (file) )” always wrong?

And you never check if this line succeeds:

fgets(line,500,file);

Also, you don't close file:

fclose(archivo);
Community
  • 1
  • 1
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

Depending on your input, token2 = strtok(<...>,":"); can return a NULL value, and the next line gives a segfault while trying to copy NULL. You can add a check for token2, and break if it's NULL. Something like:

fgets(line,500,file);
token2 = strtok(line,":");
if(!token2)
    break;
ilke444
  • 2,641
  • 1
  • 17
  • 31