【问题标题】:Segmentation Fault when reading File读取文件时出现分段错误
【发布时间】:2015-03-12 20:15:41
【问题描述】:

我正在读取一个文件,其中存储了我以后必须绘制的信息。当我阅读文件时,我可以收集我需要的信息(使用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)

【问题讨论】:

  • 我猜房间是 NULL 并要求mcve
  • 显示调用代码

标签: c file-io fgets


【解决方案1】:

fgets() 仅在无法读取字符时返回 NULL。

您需要做的是检查 feof(file)。当您完成读取文件时,EOF 位设置为 1,并且 feof() 返回 true;

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多