Thread: bmp files

  1. #1
    Registered User
    Join Date
    Dec 2017

    bmp files

    i have a bmp file; i declared some structures

    struct bmp_fileheader
    I read the information:
    struct bmp_fileheader prim;
    fread(&prim, sizeof(prim), image);
    but now i have to write all of this info into another file. I opened it
    file = fopen (image2.bmp, "wb");

    butI don t know how to use fwrite in otder to wite all the information stored in my structure. Can someone please help me?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    If you can use fread, you can use fwrite. They're virtually identical, except that one reads and the other writes. Same basic parameters, same order: pointer to data, item size, number of items, file.

    However, you're using fread wrong. It takes 4 parameters, and you're only providing 3. See the documentation.

    Your compiler should have warned you about that. You need to make sure you're #include'ing the proper headers in your program and have turned on your compiler warnings. In fact, set them to the strictest warnings possible, especially as you're learning to program. It will help you find errors.

    You also should be very careful about using fread and fwrite to write entire structs, as you can run into issues with struct member alignment and padding. Check these two links for more info. You probably want to read the header field-by-field into the individual struct members.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2012, 03:25 PM
  2. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM

Tags for this Thread