Okay, if you're still with me. I managed to write both type of string and of integer. I figured how to read back the type of string, but came up with nothing for type of integer after hours. Any idea
Code:
// codes to write string d.name
len = static_cast<int>(d.name.length());
strncpy(str, name.c_str(), len);
stream.write(reinterpret_cast<const char *>(&len), sizeof(int));
stream.write(reinterpret_cast<const char *>(str), len);
return stream;
//codes to read string d.name and set it
stream.read(reinterpret_cast<char *>(&len), sizeof(int));
stream.read(reinterpret_cast<char *>(strTmp), len);
strTmp[len] = '\0';
d.setName(strTmp);
// codes to write int d.num
outStream << d.num;
len = static_cast<int>(outStream.str().length());
strncpy(str, outStream.str().c_str(), len);
stream.write(reinterpret_cast<const char *>(&len), sizeof(int));
stream.write(reinterpret_cast<const char *>(str), len);
outStream.str("");
// codes to read int d.num back????
Note that I use write() because I write the file in binary mode