user861938
user861938

Reputation: 83

Store customer data into a binary file in c++

I am creating a banking system in c++. I have created all the functions like deposit,withdraw.Different account types: saving and current. But dont know how to store the data in a binary file. Also i want that the data entered in one session must be present for use in the other. Can you please suggest me something how to do it?

Upvotes: 0

Views: 345

Answers (2)

thelamb
thelamb

Reputation: 486

Seems what you're trying to do is actually serialization. Good libraries to do this are Boost.Serialization or Protocol Buffers.

Boost.Serialization is a complex monster with lots of caveats to consider, but for your simple case it should be fairly easy to do. Protocol Buffers requires some more work from a maintenance perspective (you have to maintain a .proto file that describes the format of what you're saving).

I can go into a lot of detail here about how each library works but I suggest you look at the documentation to see which one suits you best. Personally, I used to work with Boost.Serialization a lot but because of all the caveats I moved to Protocol Buffers (also be cause I use it to transfer objects between Java and C++).

With these libraries you can easily save the objects in binary format, without worrying too much about the details. At a later point, you can also easily switch to an XML or text output.

Upvotes: 0

James Kanze
James Kanze

Reputation: 153977

First: why binary? Binary files are usually not a good idea.

If it does have to be binary, you'll have to start by defining a binary format: how you want to represent each type in the file. Binary does not mean unformatted. Unless there are good reasons for doing otherwise, you might start with an already defined format, such as XDR. Then you should probably define stream types for reading and writing this format—the input and output functions in std::istream and std::ostream either use text format, or are unformatted (which means you have to manually format or unformat the data you give it or receive from it). It's probably a good idea to have these classes derive from std::basic_ios<char>, however; it has a lot which will be superflous (e.g. the formatting flags), but it will provide a more or less standard error reporting mechanism. And you can (and probably should) use the standard streambuf, just ensure that with filebuf, the file is opened in binary mode, and the filebuf itself is imbued with the "C" locale.

Alternatively, you can format into an std::vector<unsigned char>, and use the system level functions to do the writing. In fact, you may have to do this, since filebuf cannot be made transactionally secure. If this is a real bank application, and not course work, you will have to do this, because real bank applications do require transactional integrity: you have to open the file with special arguments, to ensure that you don't return from a write until the data is physically on disk. And none of the standard streams support this.

Upvotes: 3

Related Questions