I'm trying to read and write a std::map object to/from a file. the map type is
map<string, Node*>
and i have successfully written it to a file but unsuccessfully read it back. i'm not sure if i'm storing it correctly i think it's because i have pointers in the map (Node*) but i'm not sure. how can i fully write the entire map with all the objects it contains and then read it back perfectly. my current read/write methods are
Read
template<typename T>
T ReadObject(string path) {
T num;
ifstream infile;
infile.open(path, ios::in|ios::binary);
infile.read(reinterpret_cast<char *>(&num),sizeof(T));
infile.close();
return num;
}
Write
template<typename T>
void WriteObject(string path, T& num) {
ofstream outfile;
outfile.open (path, ios::out|ios::binary);
outfile.write(reinterpret_cast<char *>(&num),sizeof(T));
outfile.close();
}
btw these work when reading and writing integers
Pointers are just memory addresses. The data you're pointing to is never being placed into the file.
You need to use what you know about the internal structure of the objects when saving them. Bitwise copy is not sufficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.