Link Between (STL) Container and (Fstream) File I/O
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Link Between (STL) Container and (Fstream) File I/O

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Link Between (STL) Container and (Fstream) File I/O

    Greeting Codeguru world.
    A small question in my mind is ringing me time to time. I did some of the projects using STL container and it is pretty much efficient to retrieve the data's information quickly (i.e. sets, sets & maps). I read many C++ OOP book and none of them make me clear about the Link Between (STL) Container and (Fstream) File I/O. Can they link together ?

    Making clear,

    I am going to design a small Library Book Management Project. I am planning to develop it using the STL Algorithms & containers. Objective of this project is to keeping the records of books issued and deposited by students in a particular date. User will be given a facility to retrieve student's information, book information and he/she can modify/update specific book's, student's information by changing issue date, deposited date, Book's publication etc.

    My intention is to create a File I/O where user can input & update/modify daily transaction inside the file called (xx.dat) file in project directory. Is it possible to create 'set' container inside the file (xx.dat) ? Or, It's impossible ? Or, do i have to do this in other way?

    Any suggestion ?

    I hope i make cleared what i wanted to say?

    Regards
    basanta

  2. #2
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Link Between (STL) Container and (Fstream) File I/O

    if I get you correctly, what you're looking for is called "serialization" and it's not supported by default by STL containers. That is, in general, there's no standard way of "writing" or "reading" a c++ object back and forth from a textual, binary or whatever storage or transmission mechanism you can think of. You have to develop a system yourself ( this can be as simple as writing an iostream operator ) or use some existing library.

    And this is so for at least two reasons: first, the c++ memory object model is not sufficiently constrained to give a fixed binary representation of any type instance, especially non-POD's; second, it's up to you ( the creator of the type ) to decide what's the logical content of a type and how this is represented internally; for example, consider a std::list: typically, the container refers to nodes and nodes refer to each other via pointers, whose binary value obviously changes from run to run; hence, storing their binary values would be useless without some further machinery ...

    regarding existing libraries, take a look at boost serialization: it has a ready-made support for serializing STL containers.

    As I side note, your use case seems ideal for a database; have you considered something like SQLLite instead ?

  3. #3
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Link Between (STL) Container and (Fstream) File I/O

    Thanks for the response, and i got to know a bit what is serialization. My intention is to save the object in binary form in file.

    Quote Originally Posted by superbonzo View Post
    take a look at boost serialization: it has a ready-made support for serializing STL containers.
    So, the library from boost serialization can read the binary code in file and display it ? Does it allow to edit the object in file in character mode when it is actually saved in binary form in file.

    Quote Originally Posted by superbonzo View Post
    As I side note, your use case seems ideal for a database; have you considered something like SQLLite instead ?
    I am beginner programmer and i am just learning basics of C++ using console application via Code::Block IDE, GNU GCC Compiler.

    Thanks in advance

  4. #4
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Link Between (STL) Container and (Fstream) File I/O

    Quote Originally Posted by basanta View Post
    So, the library from boost serialization can read the binary code in file and display it ?
    yes, among other things, it can load and save an STL container instance from a binary file ( provided the container value type is serializable itself ), see boost doc for details and examples.

    Quote Originally Posted by basanta View Post
    Does it allow to edit the object in file in character mode when it is actually saved in binary form in file.
    it depends on the serialization "target" ( called an "archive" in this library ); if you need to view or edit the resulting file through standard means ( like, say, a browser ) you can save to xml, for example.

    Quote Originally Posted by basanta View Post
    I am beginner programmer and i am just learning basics of C++ using console application via Code::Block IDE, GNU GCC Compiler.
    I see. Well, then serialization could be an overkill for you and, anyway, an SQLlite solution could be still the way to go if you have basic database knowledge, being more appropriate for your use case.

    Alternatively, if you're just exercising your c++ skills then you could simply use iostream to store and populate your sets, maps, etc... from file, manually item by item. IMHO, not worth the effort though.

  5. #5
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Link Between (STL) Container and (Fstream) File I/O

    Quote Originally Posted by superbonzo View Post
    Alternatively, if you're just exercising your c++ skills then you could simply use iostream
    I already exercised my C++ skills in File I/O applying Array Index Container. Afterward, i asked myself, "Is it possible to applying STL Container?". So, my plan slightly changed to change the container.

    Quote Originally Posted by superbonzo View Post
    store and populate your sets, maps, etc... from file, manually item by item. IMHO, not worth the effort though.
    "So, Manually, item by item" I didn't understand. Does it means, don't it need library from boost serialization ?

  6. #6
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Link Between (STL) Container and (Fstream) File I/O

    Quote Originally Posted by basanta View Post
    "So, Manually, item by item" I didn't understand.
    here is a simplified example:

    Code:
    #include <map>
    #include <iostream>
    
    typedef std::map<int,int> MyMap;
    
    void write_some_map_to_file( MyMap const& map, std::ostream& stream )
    {
    	// write map pairs in textual format
    
    	for( MyMap::const_iterator it = map.begin(); it != map.end(); ++it )
    	{
    		stream << it->first << ' ' << it->second << std::endl;
    	}
    }
    
    void read_some_map_from_file( MyMap& map, std::istream& stream )
    {
    	// read map pairs from the same textual format
    
    	MyMap::key_type key;
    	MyMap::mapped_type value;
    	
    	while( stream >> key >> value )
    	{
    		map[ key ] = value;
    	}
    }
    try experimenting yourself, you'll see that code like this can easily become unmanageable in more complex scenarios.

  7. #7
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Link Between (STL) Container and (Fstream) File I/O

    Just reviewed the tutorial and i got the following information :

    "STL Collections

    The serialization library contains code for serialization of all STL classes. Hence, the reformulation below will also work as one would expect."

    Code:
    #include <boost/serialization/list.hpp>
    
    class bus_route
    {
        friend class boost::serialization::access;
        std::list<bus_stop *> stops;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & stops;
        }
    public:
        bus_route(){}
    };
    Is this tutorial is correct one, which i may need to read & write STL classes in file ? If yes, this syntax is a bit complex for me? Can any one rebuild it in simple & understandable form?

    Regards
    Basanta

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center