struct element
{
std::string filename;
std::string hash;
bool isLocalized;
};
to a file, to read it later, in another program (so i cant just store addresses)
This works well when i change the type to char* instead of strings, but i'd like to know if there is a way to use strings :D (without libraries like boost or anything)
writing :
1 2 3 4 5 6
std::vector<element> v;
// Here loop to add elements to v
std::ofstream ofs("manifest.txt", std::ios::binary | std::ios::trunc);
if (!ofs.is_open()) return -1;
ofs.write(reinterpret_cast<char*>(v.data()), v.size()*sizeof(element));
ofs.close();
struct manifest_element
{
std::string filename;
std::string hash;
bool isLocalized;
};
// write contents of the structure to an output stream as plain text
// each object is written as three lines of text. format:
// line 1: filename
// line 2: hash
// line 3: isLocalised
// (assumes that filename and hash do not contain embedded new line characters,
// if that is not the case, a different delimiter has to be used.)
std::ostream& operator<< ( std::ostream& stm, const manifest_element& elt )
{
return stm << elt.filename << '\n' << elt.hash << '\n'
<< std::boolalpha << elt.isLocalized << '\n' ;
}
// read contents of the structure from an input stream
// input format is the same as what is written to the output stream
std::istream& operator>> ( std::istream& stm, manifest_element& elt )
{
std::string filename;
std::string hash;
bool isLocalized;
if( std::getline( stm, filename ) && std::getline( stm, hash ) && stm >> std::boolalpha >> isLocalized )
{
elt = { filename, hash, isLocalized } ; // set the fields
stm.ignore( 1000, '\n' ) ; // extract and discard the newline after the last field
}
else elt = {} ; // input failed; clear all the fields
return stm ;
}