tags:

views:

148

answers:

2

Possible Duplicate:
How to output array of doubles to hard drive?

Hello everyone,

Sorry for asking the same question twice, I'm new at this and still learning how to use this site properly.

I want to store an array of floats/doubles to my hard drive in the form of a file (.txt .dat .bin, whatever, it doesn't matter).

I want that file to be a mirror image of what the original array looked like as it was sitting in the RAM. I also want to know how to read that data quickly and efficiently back into a program (basically copy that chunk of data from hard drive directly back into RAM and let the program know that "this" is an array of doubles).

Lastly, since I'm outputting huge volumes of data (up to a couple hundred MB, depending on the case) I need to be able to do this fast and efficiently.

Thanks in advance,

-Faken

A: 

You could use SQLite, which provides easy access to using SQL in your program. Here you'll find the amalgamation.

Also, you could check out this beautiful C++ wrapper around SQLite. In this way, you can save your data to HDD in no time and you can use efficient queries as well to fetch the data.

nhaa123
+2  A: 

Here's some basic code to get you started. Of course, you'll want to add error checking, etc. Also, you'll have to decide whether you want buffered or unbuffered IO. Another (non-portable, but possibly faster) approach is to bypass STL and go directly to the operating system. Finally, I don't know boost, but I'm getting the impression that just about anything you could think of wanting is there, so you may want to look there too.

#include <fstream>
#include <string>

using std::ofstream;
using std::ifstream;
using std::string;

void WriteDoubleArray(const double arr[], size_t size, const string fileName)
{
   ofstream outFile(fileName.c_str(), std::ios::binary);
   outFile.write((const char *)arr, size * sizeof(arr[0]));
}


void ReadDoubleArray(double arr[], size_t size, const string fileName)
{
    ifstream inFile(fileName.c_str(), std::ios::binary);
    inFile.read((char *)arr, size * sizeof(arr[0]));
}
Ari