Thread: Storing objects after program terminates

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Storing objects after program terminates

    Hello,

    I am writing a program which is performing some computer vision and image processing tasks. I have a database of about 100 images, from which I extract some data (properties of the images). Once I have extracted this data, I then perform some analysis on it.

    So far, every time I run the program, it extracts the image data each time, and stores the data in an image_data structure that I have written. However, it is very time consuming to get this data every time, and seems silly to do it every time because it is the same data I am storing each time.

    So, I am looking for a way to store this data this data somewhere that can be found each time I run the program, without having to extract the data again. I'm sure this sort of thing happens a lot, so I'm wondering what the possible solutions are?

    One way I thought of is to print out all the data to a text file, save it on disk, and then access the file every time I run the program.

    Ideally, however, I would like to have the image_data structure like before, and be able to access it from my program. Normally, the image_data structure is obviously deleted when my program terminates...but is there a way I can have some sort of class which is written to permanently, and is loaded every time the program runs?

    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You just need to serialize your data structures. Depending on how the data is stored, it might be very easy, or it might take a little bit of effort.

    Can you give us an idea of how the data is stored? If it is stored in objects of a class, what are the member variables of that class? What kind of data structure are you using to hold the objects?

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    36
    Hi, thanks for the help.

    Here is the structure:

    Code:
    struct Image_Data
    {
    public:
            Detection_Data d_data;
    	int x;
    	int y;
    	double scale;
    	double orientation;
    	double descriptor[128];
    
    };
    So it contains some integars, some doubles, one double array, and also one other structure, called Detection_Data, which just contains some integars:

    Code:
    struct Detection_Data
    {
        int a;
        int b;
        int c;
    };
    Thanks for the help - serialization sounds like a good approach!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since those are all plain old datatypes, then writing and reading in binary mode seems like the easiest and most logical way to do it. Using ofstream/ifstream with ios::binary and write()/read() should be all you need to do.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Daved View Post
    Since those are all plain old datatypes, then writing and reading in binary mode seems like the easiest and most logical way to do it. Using ofstream/ifstream with ios::binary and write()/read() should be all you need to do.
    Not if you care at all about portability. The layout of PODs in memory (eg order of bits, number of bits, padding in structs) is implementation defined. So the format of a binary file varies between systems (unless you put specific additional effort into defining a portable binary format that suits your application).

    The common alternative is to output the data as text. The I/O is slower than for binary I/O, but is also (more) portable by default.

    If you don't care at all about portability, then writing and reading in binary mode is fine.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    36
    Hi,

    Ok I've tried doing the object serialization, but it doesn't quite work. Here's what I have:

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    class My_Class
    {
    public: My_Class(){};
            My_Class(int a, int b)
            {
                x = a;
                y = b;
            };
    
    private:
        int x;
        int y;
    };
    
    int main()
    {
        My_Class my_class1(2, 5);
        ofstream ofs("D:\\my_class.ros", ios::binary);
        ofs.write((char *)&my_class1, sizeof(my_class1));
        My_Class my_class2;
        ifstream ifs("D:\\my_class.ros", ios::binary);
        ifs.read((char *)&my_class2, sizeof(my_class2));
    }
    It compiles fine, and it writes ok to the .ros file. However, when I read this file into my_class2, it doesn't set the variables of my_class2 to be equal to those of my_class1, like I want it to. It doesn't set my_class2.x and my_class2.y to anything - they are left at -85983560 or something...

    Any ideas on what I am doing wrong? I followed this example from a tutorial and I can't see any differences between my code and theirs...

    Thanks!

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try closing your output stream before opening the file as an input stream.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    36
    Ah yes that works, thanks grumpy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM