Okay, this might become a monstous post, and I apologize for that. I recently switched from programming Java to C++ like a week ago, and can't seem to get all the pointers right it seems.
Maybe all code provided in this post might seem a little overkill for the size of my problem, but I'm not sure if this problem really is as small as it seems, or if there are other steps along the process that cause this to happen. Maybe you would like to start looking at my outputs first.
The project I'm working on is going to bundle lots of data together into binary packages (both numbers, strings, images and later on sound aswell). I want to do this because I'm making my own game console or so to speak, and I want each of my games inside just one file. Hopefully you get the point.
I've managed to successfully write and read som animation info and some images, but something wasn't right. Sometimes when I compiled and runned the program would crash when upon loading the game data. So I did some research and I think I found the problem. However, first I wish to introduce you to some of my methods to help you understand my problem. They are somewhat rewritten for the purpose of this post, but I tried to preserve the running circumstances of my original program:
To make everything easier, I made a library for some reading and writing:
// Writes a string outStr to BIN file ofs
void stringToBINFile(string *outStr, ofstream *ofs)
{
int strLen = strlen(outStr->c_str()); //Length has to be known when reading
ofs->write((char *)&strLen, sizeof(strLen));
cout << "Wrote string length: " << strLen << endl;
cout << "Wrote char: ";
for (int i = 0; i < strLen; ++i)
{
ofs->write((char *)&outStr->c_str()[i], sizeof(outStr->c_str()[i]));
cout << "'" << outStr->c_str()[i] << "' ";
}
cout << "\n";
}
// Reads a string inStr from a BIN file ifs
void stringFromBINFile(string *inStr, ifstream *ifs)
{
int strLen;
ifs->read((char *)&strLen, sizeof(strLen));
cout << "Read string length: " << strLen << endl;
inStr = new string;
cout << "Read char: ";
for (int i = 0; i < strLen; ++i)
{
ifs->read((char *)&inStr->c_str()[i], sizeof(inStr->c_str()[i]));
cout << "'" << inStr->c_str()[i] << "' ";
}
cout << "\nFinal string: '" << inStr->c_str() << "'. Len: " << strlen(inStr->c_str()) << endl;
}
Then I wrote two test programs, one to compile a BIN file:
void myWrite(char* path, string* outStr)
{
ofstream ofs (path, ios::binary);
stringToBINFile(outStr, &ofs);
ofs.close();
}
int main()
{
string outString = "WATER";
myWrite("test1.bin", &outString);
outString = "SNOW";
myWrite("test2.bin", &outString);
}
And one to read from the BIN file:
void myRead(char* path, string* inStr)
{
ifstream ifs (path, ios::binary);
stringFromBINFile(inStr, &ifs);
ifs.close();
}
int main()
{
string inString;
myRead("test1.bin", &inString);
cout << inString << endl;
delete &inString;
myRead("test2.bin", &inString);
cout << inString << endl;
}
Output from write program:
Wrote string length: 5
Wrote char: 'W' 'A' 'T' 'E' 'R'
Wrote string length: 4
Wrote char: 'S' 'N' 'O' 'W'
Okay, so far so good. 'test1.bin' exists, and seems to contain the amount of data I wrote to them. However (finally!) I get to the intresting output of my read program:
Read string length: 5
Read char: 'W' 'A' 'T' 'E' 'R'
Final string: 'WATER'. Len: 5
WATER
Read string length: 4
Read char: 'S' 'N' 'O' 'W' 'R'
Final string: 'SNOWR'. Len: 5
SNOWR
EDIT: Corrected order of output
In the final read it seems my string preserves its length from its previous data. For all I know there should be nothing left of the string after I've deleted and allocated a new string to it. Yet it seems to remember its old length and fill out with whatever characters it's missing. Am I not deleting it enough? Or am I simply doing it wrong? I've tried delete [] &inString;
but with no success.
Also, this code is not my actual compiled version because I had to cut lots of it down and get rid of parts that are not related to this problem. Therefore you might find errors like a missing * or something somewhere. I ask you to overlook those things (think of it as pseudo code :)) And only point it out if it might actually be what's causing the problem.
Thanks a lot for your time, and it would mean a lot to me if we managed to sort this out!
Emil Olofsson