DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8

Thread: serialization

  1. #1
    Join Date
    Jul 2007
    Posts
    18

    serialization

    Hello again!
    i am trying to store a struct into a file. The struct is:
    Code:
    struct single-event{
    	eca event_name;
    	vector<string>params;
    	void WriteSingleEvent(ofstream& of);
    	
    };
    where "eca" is an enum.

    what i did in order to serialize this struct is :

    Code:
    template<typename T>
    void Output(std::ofstream& o, T& t){
    	o.write(reinterpret_cast<char*>(&t),sizeof(T));
    }
    
    void OutString(std::ofstream &o, const std::string &s)
    {
    	int size=s.size();
    	Output<int>(o,size);
    	o.write(s.c_str(),size);
    }
    void single-event::WriteSingleEvent(ofstream& o){
    	of.write(reinterpret_cast<char*>(&event_name),sizeof(event_name));
    	Output<long>(o,params.size());
    	std::for_each(params.begin(),params.end(),std::bind1st(OutString,o)); 
    }
    now i really want your opinion. is this right?
    thank you very much!:WAVE:
    Last edited by nadiap; 08-23-2007 at 02:27 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    serializing a vector of strings requires that the strings in the vector be flatten into a sequence of char arrays, possible seprates by a certain token. Alternative you cna write the size of each string as the first word, and then read the same number of characters, in a loop. Remember: serialization has to be done with desrialization in mind so you have to devise a protocol that knows how the data are stored in the file.
    Danny Kalev

  3. #3
    Join Date
    Jul 2007
    Posts
    18
    Danny thank you for your reply. Actually isn't this what i actually do?
    I mean i do write the size of each string through
    Code:
    Output<long>(o,params.size());
    std::for_each(params.begin(),params.end(),std::bind1st(OutString,o));//do i use the std::bind1st function right?
    since the OutString function is:
    Code:
    void OutString(std::ofstream &o, const std::string &s)
    {
    	int size=s.size();
    	Output<int>(o,size);//stores the size of each string
    	o.write(s.c_str(),size);//stores the string itself
    }
    and the simple Output function:
    Code:
    template<typename T>
    void Output(std::ofstream& o, T& t){
    	o.write(reinterpret_cast<char*>(&t),sizeof(T));
    }

  4. #4
    Join Date
    Jan 2007
    Posts
    145
    function pointer cannot be serialized.

  5. #5
    Join Date
    Jul 2007
    Posts
    18
    could you please be more certain?
    what function pointer?
    thank you.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    No, that's not what your program does. You're (probably) trying to cast a whole string object and treat as if it were the char array that the string contains. Hint: you don't need reinterpret_cast or a template. Simple use write(str.c_str()).
    Danny Kalev

  7. #7
    Join Date
    Jul 2007
    Posts
    18
    Danny thank you once again.
    Would i ask so much if i told you to explain further?
    i don't exactly understand what you suggest and why my code is wrong.
    I am really stuck.
    Thank you. Sorry for the insistence...

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I discussed serialization of strings here:
    http://www.devx.com/getHelpOn/LegacyLink/9487
    Danny Kalev

Similar Threads

  1. Replies: 0
    Last Post: 02-24-2006, 07:37 AM
  2. Replies: 5
    Last Post: 03-21-2003, 02:58 PM
  3. Replies: 0
    Last Post: 10-08-2002, 12:24 AM
  4. Custom Serialization of Base Class
    By John Hauppa in forum .NET
    Replies: 1
    Last Post: 03-04-2002, 01:15 PM
  5. Replies: 0
    Last Post: 08-27-2001, 08:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links