The problem:
Reading/Writing a binary data file that contains several variable length strings as well as binary data.
Code:
std::ofstream("CraftTest.dat",std::ios_base::binary);
...
...
//Write the string and its null terminator
outFile << m_CraftName.c_str();
//Skip past the null
outFile.seekp(1,std::ios_base::cur);
//Write binary data
outFile.write((char *)&m_CraftData,sizeof(BinaryCraftData));
What happens here is that the null terminator is written to disk, however, I must do the seekp because the stream position is not incremented via the terminating null. So without the seekp when the next write happens it blows away the null terminator.
However when reading the data file back in nothing works correctly except reading in the string. After that point none of the data following the string is correct even if I do a seekg() to get past the null.
How can I read in this variable length string without writing the length to the file? I would prefer to do this without having to manually read in each character until I encounter a null. My initial thoughts were that if I wrote the string to disk correctly with the null that this would work.
Code:
std::ifstream("CraftTest.dat",std::ios_base::binary);
...
...
//Read the string and it's null terminator
inFile >> m_CraftName;
//Read in binary data
inFile.read((char *)&m_CraftData,sizeof(BinaryCraftData));
This reads in the string correctly but now every read after this point is pure garbage as if my file position is off causing me to get some very weird numbers. Just in case the string read was not reading the null I tried to do a seekg() after it to skip the null and this still did not fix anything.
If I had just used C I/O I would have been done by now.