tags:

views:

69

answers:

4

Hi

Can somebody give me some tips about storing a lot of data into a file?

For example: I'm creating an audio sequencer with C++, and I want to save all the audio sample names (the file paths), info about the audio tracks in the project (name, volume, mute, solo, etc.) and where the samples are placed on the timeline into a file.

I really have no idea what's the best way to do this. I don't want to use 3th party library's for this, and I'm a beginning programmer of the language.

Thanks!

A: 

Well... open the file in binary mode, write the data, close the file.

Or maybe you're not explicit enough about the problem?

edit> To write and read in a file in binary mode (example):

#include <fstream>
#include <iostream>

std::ofstream FileOut("Toto.txt", std::ios_base::binary);
int xout = 24;
FileOut.write(reinterpret_cast<const char*>(&xout), sizeof(int));
FileOut.close();

std::ifstream FileIn("Toto.txt", std::ios_base::binary);
int xin;
FileIn.read(reinterpret_cast<char*>(&xin), sizeof(int));
FileIn.close();

std::cout << xin << std::endl;

Now, it's just an example, you'll have a lot of valid good tutorials about that on google : http://www.google.com/search?rlz=1C1GGLS_frFR299FR303&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=c%2B%2B+binary+file

Klaim
What do you mean with *opening the file in binary mode*?
Midas
+1  A: 

Well... it's just an opinion, but next time I'll use an SQLite database as a file format.

Gregory Pakosz
What SQLite? What I want to do is creating my own file format, reading and storing data in it. Like programs as Audacity for example do.
Midas
it's a embeddable SQL database engine
Gregory Pakosz
A: 

When you want to save different information in the same file, there are two popular ways to go: fixed-length fields and delimited fields. With fixed length field, each part is stored in the same size chunk. So if you wanted to store 5 things, and you store them in 80-character blocks, you can go to offset 160 in the file to read the third one.

In delimited files, you put a character (or series of characters) between each piece of data, which can be of any length. Since your data can vary greatly in length, I would suggest using delimited storage, probably with each one on a separate line ("\n" printed between each one).

dj_segfault
+2  A: 

Two other ideas come to my mind:

1) Use an XML type format

2) Use a windows initialization file format

David Harris
I think I will take a look at INI files. Thanks.
Midas
The problem is: it's not really meant to be a configuration file for the program. I really want to create my own file format that stores data in maybe an easy way so it's editable in Notepad, or like professional programs do like Photoshop, Cubase ... I want to go very standard with this.
Midas
If you like standards, XML is a good way to go, although to do it right you need to use an XML library (hand-generating/parsing XML is a recipe for unnecessary pain). (Note that just using XML isn't usually enough to make your file format usable by other software, though) If you just need something simple, a plain-text format has the advantage of being easy to implement (no 3rd party libraries required), and easy to check for correctness when debugging.
Jeremy Friesner