Hi Daniweb users,

I am attempting to make a database in c++ by saving the information to a class and then saveing it to a file.

but i dont know if that is a good way to do it.

Here is a example picture of the database i am making : http://img835.imageshack.us/img835/4648/databaseexample.png

More details of the method i intent to use :

When the user inserts the information into the textboxes the information gets saved(and also reads) in a file.txt like this :

{MEDatabaseid=5}
{
name=Carl
lname=Johnsen
}

thats the way i intend of making a database in c++ but i dont know if there is a better way you guys recommend me.

Recommended Answers

All 4 Replies

From what you have posted, it doesn't really seem like you are really making what most people call a DataBase (which generally uses a database server using some SQL queries and such. Examples include MySQL, Oracle, etc.).

In your case, this is what most programmers would call a memory serializer. Anyhow, when saving to a file, you have to consider how easy it is to save, how easy it is to load (or parse) the file, and how much complex the objects can be. Generally, the XML syntax is quite good and there are several open-source libraries for loading and saving XML files (or you can do like I do, which is to make your own library, because the XML format is very simple but also very flexible).

There are probably lots of way to do it. One of the easiest is to create a fixed-length structure then save it to the file in binary format

struct person
{
   char lname[40];
   char fname[40];
   char mi[3];
   int id;
   // other fields here
};

// save to a file
int main()
{
   Person p; // assume p is populated with some data
   ofstream out("filename.dat",ios::binary | ios::ate);
   out.write((char)&p, sizeof(Person));
}

[edit]Mike's suggestion is more appropriate if the records in the file need to be shared wih other programs wich already know how to read/write XML files. Writing files as I suggested would restrict those files to only the program(s) you write, and it is more difficult to change (add or remove) items in the structure.

I am making a database that is just for the programs i write and stays localy on the pc(offline).

So you guys would recommend using SQL queries?

>>So you guys would recommend using SQL queries?
NO, I was just mentioning this because it is part of what IT people call a database.

I would recommend using the Boost Serialization library if you want to be trouble-free. But this uses template code a lot, so it might be hard to use if you are not proficient with that.

If all you want is save/load data structures to a file(s). First, you need to choose whether you want a binary or ascii file, i.e. computer-readable or human-readable, respectively. Then you need to know if you need a lot of classes and structs to be saved and loaded, or if it is just one type of data (a list of small data structures). Finally, you need to determine if you have an object graph to save/load (an object graph being a set of objects that use each other via pointers or references).

The Boost Serialization library is geared for the most complicated case (both binary and ascii, large object graph with many heterogeneous types). But it can also easily serve the simplest case (binary, one type of data structure, no object graph). I have my own serialization library that pretty much does the same as the Boost one (but a bit differently), for both binary and XML.

Basically, the way you had it in the original post is quite alright for the basic case. You might want to think and make sure the format is robust to when a person might open it with notepad and change the content manually. If you add spaces or empty lines, will it still be loaded correctly?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.