   Problem with binary reading
  HiyaI am making a program that will read and save binary data, the writing causes no errors but the reading won't work. The item I am trying to save is simple, at the moment it only contains a vector of strings. This is the code for reading from the file:{    Info myText;    ifstream fin(fileName, ios::binary);    std::cout << "\nmark 1\n";    if(!fin)    {   std::cout << "\n\n\nSomething has gone terribly wrong!!!\n\n";   getchar();    }    std::cout << "\nmark 2\n";    fin.read((char*) &myText, sizeof myText);    std::cout << "\nmark 3\n";    fin.close();    std::cout << "\nmark 3.1\n";    std::cout << myText.Text.size();    std::cout << "\nmark 3.2\n";    std::cout << myText.Text[1];    std::cout << "\nmark 4\n";    return myText.Text[1];}This is the output:mark 1mark 2mark 3mark 3.11mark 3.2<up comes annoying windows error message>Now firstly I'm sure someone will point out that I should test if that I'm not going past the end of the vector, and I am going to add that once there will be an unknown quantity in the vector. When I saved the object it had 1 string in the array. Printing the string size shows that. But why can't I access the string that was saved?   
  Also I tried making the vector of strings into just a string, but still as soon as I try to access the data in the string the program breaks.   ;  [edit2] Err, Enigma probably has it right.Can you give us the code for the Info class? I suspect that you are using uninitialized character pointers (char *). [edit] No, that's not it. Are you sure your vector is of size 2? Note that the first element in a vector is at myText.Text[0].jfl.   ;  You haven't really given us enough to go on.  What is the definition of Info?  What is the format of the file?  That said, the fact that you're using a single read call to read into an object that looks like it contains at least a dynamic array certainly rings alarm bells.  You need to serialize and deserialize dynamic data properly, you can't just write out the raw bytes and read them in again.Σnigma   ;  I didn't think you needed to do anything special with a class if you wanted to save it in binary format (unlike Java and C#). class Info{public:    /*vector<string> Text;*/    string Text;};;  Yeah, read Enigma's link.You may find Boost.Serialization interesting.jfl.   ;  Oh sorry about giving you that source code, I was just trying everything I could think of, it didn't work when I used "myText.Text[0]".Heres a version of the code as it is, I've scrapped the vector and just tried to use one string.class Info{public:    /*vector<string> Text;*/    string Text;};string GetBinFileContents(char fileName[]){    Info myText;    ifstream fin(fileName, ios::binary);    std::cout << "\nmark 1\n";    if(!fin)    {   std::cout << "\n\n\nSomething has gone terribly wrong!!!\n\n";   getchar();    }    std::cout << "\nmark 2\n";    fin.read((char*) &myText, sizeof myText);    std::cout << "\nmark 3\n";    fin.close();    std::cout << "\nmark 3.1\n";    //std::cout << myText.Text.size();    std::cout << "\nmark 3.2\n";    std::cout << myText.Text;    std::cout << "\nmark 4\n";    return myText.Text;};  Ah, I think I need to include the strings length in the binary file. If I read the length from the binary file how then woulf I use that to read the string?   ;  The problem is, string looks something like this (extreme oversimplification):class string{public:// public interfaceprivate:char * begin;size_t size;};So, when you write it out to disk using write you only write out a pointer and an unsigned integral type.  You don't write out the actual contents of the string:your string object+--+--+--+--+--+--+--+--+| 0x04000000| 0x00000003|+--+--+--+--+--+--+--+--+^|address 0x01000000the actual string data+--+--+--+| C| A| T|+--+--+--+^|address 0x04000000So, when you read it back in you end up with, e.g.:your string object+--+--+--+--+--+--+--+--+| 0x04000000| 0x00000003|+--+--+--+--+--+--+--+--+^|address 0x01008000the data pointed to. this is whatever happened to be in memory at the time+--+--+--+| H| J| Z|+--+--+--+^|address 0x04000000Even worse, the address pointed to by your string object might not even be valid memory.The correct approach is to serialize and deserialize the dynamic data (example of binary serialization/deserializarion):void serialize(std::ostream & stream, std::string const & string){// write how many bytes are in the stringstd::size_t length = string.size();stream.write(reinterpret_cast< char * >(&length), sizeof(length));// write the string datastream.write(string.data(), string.size());}void deserialize(std::istream & stream, std::string & string){// read how many bytes are in the stringstd::size_t length;stream.read(reinterpret_cast< char * >(&length), sizeof(length));// vectors are nice for reading arrays of data intostd::vector< char > data(length);stream.read(&data[0], length);// copy the data into the stringstring.assign(data.begin(), data.end());}You'd need to do some error handling too.Σnigma   ;  ThanksWould there be any problem with using:stream.write((char *)&length, sizeof(length));stream.write(reinterpret_cast< char * >(&length), sizeof(length));