Thread: from a struct to a binary file, to a struct again

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    29

    from a struct to a binary file, to a struct again

    I am using opencv in C++ to get images from a webcam, and want to store them in a binary file. After that I want to be able to pull the data from that file into a new struct.

    This is the struct if it makes any difference:
    Code:
    typedef struct _IplImage
    {
        int  nSize;
        int  ID;
        int  nChannels;
        int  alphaChannel;
        int  depth;
        char colorModel[4];
        char channelSeq[4];
        int  dataOrder;
        int  origin;
        int  align;
        int  width;
        int  height;
        struct _IplROI *roi;
        struct _IplImage *maskROI;
        void  *imageId;
        struct _IplTileInfo *tileInfo;
        int  imageSize;
        char *imageData;
        int  widthStep;
        int  BorderMode[4];
        int  BorderConst[4];
        char *imageDataOrigin;
    }
    IplImage;
    It's an opencv struct. I've tried several methods and modified code samples for reading and writing into binary files but can't get it to work. At one point I was getting just a black screen when I tried to show the image that contained the data from the binary file, so I am guessing I wasn't reading the data properly. Now I'm just getting a segfault, which I should be able to fix. When I just show the image that I'm getting from the camera, it works just fine. It's when I try to store it in a binary file and try to pull it out from there again that I get a black screen.

    Any help would be great. Thank you in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All ints can be serialized with stream << data.
    All char arrays you need to be careful with. You can use the .write() function to write that (remember to properly specify its length).
    All structs, you can recursively apply these rules to.
    imageId I have no idea how to serialize since it could be anything.
    .write() is probably best for imageData (the size of the data it points to is probably imageSize).
    imageDataOrigin I have, once again, no idea, since there is no length that I can see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    29
    Quote Originally Posted by Elysia View Post
    All ints can be serialized with stream << data.
    All char arrays you need to be careful with. You can use the .write() function to write that (remember to properly specify its length).
    All structs, you can recursively apply these rules to.
    imageId I have no idea how to serialize since it could be anything.
    .write() is probably best for imageData (the size of the data it points to is probably imageSize).
    imageDataOrigin I have, once again, no idea, since there is no length that I can see.
    Sorry for the noobieness, but what is "stream << data"
    Also, how would the whole thing go? I simply write each field, one by one, using the ways you mentioned?

    Also, how would I go about reading it back into a new IplImage?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::ofstream stream("my file.myext", /*options*/);
    stream << data; // write
    stream >> data; // read
    Yes, you would write each field separately. It isn't safe to write it all at once. Pointers cannot be written this way.
    See documentation for streams (and/or a tutorial).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    29
    Quote Originally Posted by Elysia View Post
    Code:
    std::ofstream stream("my file.myext", /*options*/);
    stream << data; // write
    stream >> data; // read
    Yes, you would write each field separately. It isn't safe to write it all at once. Pointers cannot be written this way.
    See documentation for streams (and/or a tutorial).
    Oh, ok. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving Struct to Binary File/Copying Structs
    By timmeh in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2009, 08:44 PM
  2. Replies: 4
    Last Post: 05-12-2009, 03:28 PM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. Writing a struct to a binary file
    By Scarvenger in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2006, 01:50 AM
  5. Writing a struct to a binary file problem
    By k_w_s_t_a_s in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 10:03 AM