Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    segmentation fault

    hello. i need some help with BMP images.
    i declared 3 structures
    Code:
    struct bmp_fileheader
    {...}
    struct bmp_infoheader
    {....}
    struct bmp_bitmap
    {    int blue;
        int green;
        int red;
    };
    now i try to open the file and read from it

    Code:
    FILE *captcha =fopen("captcha.bmp", "rt");
    
    struct bmp_fileheader info; //i named my struct "info"
    
    fread(&info, 14, 1, captcha);
    unfortuantely i receive "segmentation fault". why?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your fread call should be:
    Code:
    fread(&info, sizeof(info), 1, captcha);
    You should not hardcode the size.

    Also, "rt" is an unusual file open mode. Perhaps you meant "r+"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,642
    You should open the file in binary mode, "rb".
    You should test that the file opened (a likely cause of your current trouble) :
    Code:
    if (!captcha) { perror("fopen"); exit(EXIT_FAILURE); }
    You need to either make sure your structs are packed (and that you used explicitly-sized integers) or read the members one by one. It is more portable to read them one by one.

    Code:
    #define READ(f,x) fread(&(x), 1, sizeof(x), f)
    
    typedef struct Head {
        int32_t size;   // include <inttypes.h> for int32_t and PRNd32
        int16_t planes; 
    } Head;
    
    Head h;
    READ(f, h.size);
    READ(f, h.planes);
    Last edited by john.c; 01-10-2018 at 11:42 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by laserlight View Post
    Also, "rt" is an unusual file open mode. Perhaps you meant "r+"?
    "t" in the mode string is a Microsoft extension, meaning "text" or "translated" mode.

    A bitmap file is not a text file, so it should be opened with the mode "rb".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. New to C, Segmentation fault
    By BloodX in forum C Programming
    Replies: 4
    Last Post: 04-26-2013, 02:24 AM
  3. Segmentation fault
    By ankitsinghal_89 in forum C Programming
    Replies: 2
    Last Post: 06-28-2010, 01:45 AM
  4. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread