Serialization

This is a discussion on Serialization within the C++ Programming forums, part of the General Programming Boards category; I often see classes such as the following with istream, outstream etc. I was just wondering what they are used ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Serialization

    I often see classes such as the following with istream, outstream etc. I was just wondering what they are used for and how i could use them. It has something to do with serialization right?
    I imagine print would print the object and write would be changing the data members but What would read be used for and why do they return a stream??
    Code:
    class Book:public PublishedItem
    {
    	public:
    		Book(void);		
    		string GetAuthor();
    		string getBookTypeAsString();
    		string getTypeName();
    		istream&read(istream&in);
    		ostream&write(ostream&out);
      		ostream&print(ostream&out) const;
    	private:
    		string author;	
    		enum  bookType{Fiction, NonFiction};
    };

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    read() and write() are used for binary mode input and ouput.

    generally your input and output return streams so that you can chain commands together.

    For example:
    cout<<"Hello"<<"World"<<' '<<1234;
    this relies on the fact that operator<< returns an ostream. If it failed to return an ostream then you'd have to do:
    cout<<"Hello";
    cout<<"Word";
    etc

    That'd be boring wouldn't it?

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    examples?

    is there anywhere i can find an example of how to use it? Or could you write a quick one? Do i need to override the >> or << operators?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Ok, can i do this?

    so for the example that i gave before. Just say i had a function called get() which gets the details for a book.

    i would have :
    Code:
    void Book::get(void)
    {
    ostream bookstream;
    cout<<"what is the book's title"<<endl;
    write(bookstream);
    cout<<"Who is the book's Author"<<endl;
    write(bookstream);
    
    read(bookstream); //can i pass an ostream as an i stream?? how can i do this?
    etc...
    }
    And write would be:
    Code:
    ostream&Book::write(ostream&out)
    {
    out<<cin;
    }
    And then read would be:
    Code:
    istream&Book::read(istream&in)
    {
    in>>title;
    in>>author;
    }
    Is this right?? If it is, I don't see why streaming should be used. Whats wrong with just asking the user for the title then storing it straight away?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    11
    The reason you would use streaming as opposed to writting to the disk right away is that writting to the disk is very "expensive". Relatively speaking it takes a long time to write/read to and from the disk. Streaming is a solution to this since it minimises the number of read and writes you make by waiting for a "buffer" to fill up before a read/write takes place. Of course with small read/writes you probably wont notice the change in performance.

    //can i pass an ostream as an i stream?? how can i do this?
    certainly, i believe its called piping. i would just be careful and close the file that you're writting to before you read it to make sure you're reading the right information in that file

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    database files

    so just say i had one big database file. How could i use the read/write functions in my object classes to read/write from the correct part of the file?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    With your setup you must read in the entire file, save the data in memory, change the records you want to change, and then write out the entire file.

    Since it looks like you will want to look up a book by its title, author, or whatever, I'd suggest reading in each book one at a time and storing it in a vector. When you are done reading in all the books, you can sort the vector by title, author, or whatever for a fast search.

    When you are done, you loop through each entry in the vector and write it back out to the file to save it. You could also keep indexes into your vector if you will have to switch back and forth between looking up by title, author, or whatever.

    The point is that unless you have fixed-length records, you pretty much have to read the whole thing in and write the whole thing back out every time.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Please give some more examples

    ok i understand that now. So i if i wanted to save the file, i would have to make a function called Save() for example, which would open the file and also open an ostream and then go through each of the objects in the vector, use their write method to get their details into the ostream and then i have to use the ostream to save the data right using outfile.write(out , //size?? );

    I just dont know how to go about doing it. Also how can i know which classes read function to use when i want to get the data from the file into the object?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Opening the file and opening an ostream is the same thing. You use an ofstream (with an f). An ofstream is an ostream, it is a specialized version for files. So you open your file with an ofstream, then loop through each of the objects calling its write method to get their details into the ostream (which in this case is your file's ofstream). That automatically writes it to the file.

    First try creating one object, and open a file for writing with ofstream, then run the program and check the file to see what got written out. Once that is working then try to do it with several objects. Once that is working you can then get reading in to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost Serialization unordered_multimap
    By idleman in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2009, 12:14 PM
  2. Serialization problem
    By noobcpp in forum C++ Programming
    Replies: 17
    Last Post: 07-15-2008, 09:01 PM
  3. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 08:17 PM
  4. Serialization yay!
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2007, 06:53 PM
  5. serialization and object types
    By Joe Monti in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21