By the way, I've actually improved in skill, not all my code is wierd and convoluted - here's a templated function that takes a one dimentional array, it's size in elements, and a string filename - and saves it to a file! And the read one too.
Note: I use this in my custom *.hpp toolbox header, so the names are qualified.
Code:
template <class T> bool readArray(T *array, unsigned long arraySize, std::string fileName)
{
std::ifstream file(fileName.c_str(), std::ios::binary);
if (!file.is_open()) {
std::cerr << "Unable to open \"" << fileName << "\"." << std::endl;
return false;
} else {
std::clog << "\"" << fileName << "\" opened successfully." << std::endl;
std::clog << "Reading " << arraySize *sizeof(T) << " bytes." << std::endl;
file.read((char*) array, arraySize * sizeof(T));
if (file.fail()) {
std::cerr << "Read operation failed." << std::endl;
file.clear();
return false;
} else {
std::clog << "Read " << file.gcount() << " bytes." << std::endl;
}
std::clog << "Closing \"" << fileName << "\"." << std::endl;
file.close();
}
return true;
}
Code:
template <class T> bool writeArray(T *array, unsigned long arraySize, std::string fileName)
{
std::ofstream file(fileName.c_str(), std::ios::binary);
if (!file.is_open()) {
std::cerr << "Unable to open \"" << fileName << "\"." << std::endl;
return false;
} else {
std::clog << "\"" << fileName << "\" opened successfully." << std::endl;
std::clog << "Writing " << arraySize *sizeof(T) << " bytes." << std::endl;
file.write((char*)array, arraySize * sizeof(T));
if (file.fail()) {
std::cerr << "Write operation failed." << std::endl;
file.clear();
return false;
} else {
std::clog << "Wrote " << arraySize *sizeof(T) << " bytes." << std::endl;
}
std::clog << "Closing \"" << fileName << "\"." << std::endl;
file.close();
}
return true;
}