If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

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 ?

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

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.

Originally Posted by basanta

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.

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.