简体   繁体   English

使用 fopen 时的段错误

[英]Segfault when using fopen

I am getting segmentation fault when I use fopen function.使用 fopen function 时出现分段错误。 if i use file not in struct then no error occurs.如果我使用不在结构中的file ,则不会发生错误。 i guess the segfault comes from using a file from a struct我猜段错误来自使用结构中的file

int     main(int argc, char **argv)
{
    t_paint     *paint;

    if (argc != 2)
    {
        write (1, "Error: argument\n", 16);
        return (1);
    }
    paint->file = fopen(argv[1], "r");
    if (paint->file == NULL || parsing(paint))
    {
        write(1, "Error: Operation file corrupted\n", 32);
        free(paint->map);
        fclose(paint->file);
        return (1);
    }
    output(paint);
    fclose(paint->file);
    free(paint->map);
    return (0);
}

t_paint t_paint

typedef struct      s_paint
{
    char            *map;
    int             widthMap;
    int             heightMap;
    float           firstX;
    float           firstY;
    float           w;
    float           h;
    char            typeFile;
    char            background;
    char            filler;
    FILE            *file;
}                   t_paint
t_paint     *paint;

if (argc != 2)
{
    write (1, "Error: argument\n", 16);
    return (1);
}
paint->file = fopen(argv[1], "r");

You modify the file member of the object paint points to.您修改 object paint指向的file成员。 But paint doesn't point to anything -- you never assigned it a value or created an instance of t_paint .但是paint没有指向任何东西——你从来没有给它赋值或者创建过t_paint的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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