A
adamrobillard
Hi,
I have always used fopen and FILE* to load and save structures to file.
I am trying to convert all the older code to use proper C++ calls...
the following code works properly but I would like to know if I am
using the fstream methods properly. (as long as I am cleaning up I
might as well do it right). By the way, this is just a bogus example
with a structure, values and filename for demonstration purposes.
PS-> I have a funny feeling casting my structure to a char* is the
wrong approach!
#include <iostream>
#include <fstream>
struct stAnything
{
int nInteger;
long lLong;
char cChar;
double dDouble;
};
int main(void)
{
std::string sFilename = "C:/test.bin";
stAnything stSomeStructure;
stSomeStructure.nInteger = 5; // bogus values
stSomeStructure.lLong = 10;
stSomeStructure.cChar = 'A';
stSomeStructure.dDouble = 5.1;
// Save the structure to a binary file
std:fstream outFile;
outFile.open(sFilename.c_str(), std:fstream:ut |
std:fstream::binary);
if (!outFile.is_open())
{
std::cout << "Could not open the file for output." << std::endl;
return 1;
}
outFile.write((const char *) &stSomeStructure, sizeof(stAnything));
outFile.close();
// Clear the structure
stSomeStructure.nInteger = 0;
stSomeStructure.lLong = 0;
stSomeStructure.cChar = ' ';
stSomeStructure.dDouble = 0.0;
// Load the structure from a binary file
std::ifstream inFile;
inFile.open(sFilename.c_str(), std:fstream::in |
std:fstream::binary);
if (!inFile.is_open())
{
std::cout << "Could not open the file for input." << std::endl;
return 1;
}
inFile.read((char *) &stSomeStructure, sizeof(stAnything));
inFile.close();
// Show the loaded contents
std::cout << "{" << stSomeStructure.nInteger << ", " <<
stSomeStructure.lLong << ", " <<
stSomeStructure.cChar << ", " <<
stSomeStructure.dDouble << "} " << std::endl;
return 0;
}
I have always used fopen and FILE* to load and save structures to file.
I am trying to convert all the older code to use proper C++ calls...
the following code works properly but I would like to know if I am
using the fstream methods properly. (as long as I am cleaning up I
might as well do it right). By the way, this is just a bogus example
with a structure, values and filename for demonstration purposes.
PS-> I have a funny feeling casting my structure to a char* is the
wrong approach!
#include <iostream>
#include <fstream>
struct stAnything
{
int nInteger;
long lLong;
char cChar;
double dDouble;
};
int main(void)
{
std::string sFilename = "C:/test.bin";
stAnything stSomeStructure;
stSomeStructure.nInteger = 5; // bogus values
stSomeStructure.lLong = 10;
stSomeStructure.cChar = 'A';
stSomeStructure.dDouble = 5.1;
// Save the structure to a binary file
std:fstream outFile;
outFile.open(sFilename.c_str(), std:fstream:ut |
std:fstream::binary);
if (!outFile.is_open())
{
std::cout << "Could not open the file for output." << std::endl;
return 1;
}
outFile.write((const char *) &stSomeStructure, sizeof(stAnything));
outFile.close();
// Clear the structure
stSomeStructure.nInteger = 0;
stSomeStructure.lLong = 0;
stSomeStructure.cChar = ' ';
stSomeStructure.dDouble = 0.0;
// Load the structure from a binary file
std::ifstream inFile;
inFile.open(sFilename.c_str(), std:fstream::in |
std:fstream::binary);
if (!inFile.is_open())
{
std::cout << "Could not open the file for input." << std::endl;
return 1;
}
inFile.read((char *) &stSomeStructure, sizeof(stAnything));
inFile.close();
// Show the loaded contents
std::cout << "{" << stSomeStructure.nInteger << ", " <<
stSomeStructure.lLong << ", " <<
stSomeStructure.cChar << ", " <<
stSomeStructure.dDouble << "} " << std::endl;
return 0;
}