Storing several data into a file
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 langu开发者_运维技巧age.
Thanks!
Two other ideas come to my mind:
1) Use an XML type format
2) Use a windows initialization file format
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).
Well... it's just an opinion, but next time I'll use an SQLite database as a file format.
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&sourceid=chrome&ie=UTF-8&q=c%2B%2B+binary+file
精彩评论