简体   繁体   中英

Segmentation Fault when reading File

I am reading a file that stores information that I must later draw. When I read the file, I can collect the information I need (used printf just to verify that values were being stored in correct places) but I cant seem to leave the loop.

void getInfo(info *rooms, borderControl *doors, FILE *file)
{
char buffer[150];
char tempStorage[10];
char *temp;
char *locaOne, *locaTwo;

temp = tempStorage;
temp = malloc(sizeof(tempStorage));

while(fgets(buffer, 150, file) != NULL)
{
    printf("Inside fgets loop\n");
    temp = strtok_r(buffer, " ", &locaOne);
    rooms->size[0] = atoi(strtok(temp, "x"));
    rooms->size[1] = atoi(strtok(NULL, " "));
    temp = strtok_r(NULL, " ", &locaOne);

    while(temp != NULL)
    {
        printf("Inside line loop\n");
        if (temp[0] == 'g') //gold
        {
            printf("Inside gold\n");
            temp++;
            rooms->gold[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->gold[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms, doors);
        }
        else if (temp[0] == 'h') //hero
        {
            temp++;
            rooms->heroPosi[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->heroPosi[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp[0] == 's') //staris
        {
            temp++;
            rooms->stairs[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->stairs[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp[0] == 'd') //doors
        {
            temp++;
            rooms->door[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->door[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp[0] == 'm') //magic
        {
            temp++;
            rooms->magic[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->magic[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp[0] == 'p') //potion
        {
            temp++;
            rooms->magic[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->magic[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp[0] == 'w') //weapon
        {
            temp++;
            rooms->weapon[0] = atoi(strtok_r(temp, ",", &locaTwo));
            rooms->weapon[1] = atoi(strtok_r(NULL, " ", &locaTwo));
            drawRooms(rooms,doors);
        }
        else if (temp == NULL)
        {
            break;
        }
        else
        {
            temp = strtok_r(NULL, " ", &locaOne);
            if(temp == NULL)
            {
                break;
            }
        }
        temp = strtok_r(NULL, " ", &locaOne);
    }
    printf("here\n");
}

I call this function using:

FILE *file;
file = fopen(argv[1], "r");
if (file == NULL)
{
    printf("Error! Could not open file.\n");
    return 0;
}
else
{
    initscr();
    getInfo(rooms, doors, file);
}

There isn't any compiling error/warning but when I run the program, I get this:

Inside fgets loop
Inside line loop
Inside gold
Inside fgets loop
Inside fgets loop
Segmentation fault (core dumped)

fgets() returns NULL only when characters couldn't be read.

What you need to do is check for feof(file). When you finish reading the file the EOF bit is set to 1 and feof() returns true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM