簡體   English   中英

讀取文件時出現分段錯誤

[英]Segmentation Fault when reading File

我正在讀取一個文件,該文件存儲了以后必須繪制的信息。 讀取文件時,我可以收集所需的信息(使用printf只是為了驗證值是否存儲在正確的位置),但似乎無法退出循環。

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");
}

我使用以下函數調用此函數:

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);
}

沒有任何編譯錯誤/警告,但是當我運行程序時,我得到了:

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

僅當無法讀取字符時,fgets()才返回NULL。

您需要做的是檢查feof(file)。 當您完成文件的讀取后,EOF位置1,feof()返回true;否則,返回0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM