Reputation: 246
I'm making a program which has to store places of objects. This data can reach over 100MB and I want to store it on the hard disk. Now, I searched around and there are a few ways of doing this: txt-file, sql, xml,...
I already know how to make a txt-file in C++. But what are the advantages of a .txt file to store much data? And what is the best way of doing this?
Currently I store numbers line by line so I can read it with getline()
.
But it gives me problems, it doesn't read it well.
Maybe because .txt can't handle much data or maybe because reading line by line is very slow and it gives me problems.
Someone knows how to solve this?
here is some code:
_chdir("c:/Documents and Settings/Bram/Bureaublad/cloth3d");
ofstream mfile;
mfile.open("example.txt", ifstream::out);
some calculations...
mfile << (double) vervangplaats[i][j][0] << "\n";
mfile << (double) vervangplaats[i][j][1] << "\n";
mfile << (double) vervangplaats[i][j][2] << "\n";
// the data is stored in the .txt file
mfile.close();
this stores the data in a txt file now I extract it back.
std::string b;
ifstream file("example.txt", ifstream::out);
if(file.is_open())
{
getline(file, b);
result[0][teller_x][teller_y] = (double) atof(b.c_str());
getline(file, b);
result[1][teller_x][teller_y] = (double) atof(b.c_str());
getline(file, b);
result[2][teller_x][teller_y] = (double) atof(b.c_str());
}
this last function with getline is used 2500 times a frame.
And the frames don't load, maybe because of the high amount of getline per frame or is it just because txt is not good for storing much data?
Upvotes: 0
Views: 158
Reputation: 400159
Uh, loading data from disk at all per-frame is never a good idea, that will simply be too slow. You must load the data into memory once, before you start rendering.
That said, text files are best for data that needs to be human-readable and portable.
Low-level data such as this is often stored as binary, which makes the files smaller and loading simpler and faster.
Upvotes: 3