.bin file i/o troubles

I'm having trouble with simple file io,
I'm trying to store a Word and retrieve it...

class Word{
public:
std::string theword;
int ID;
};
int main(array<System::String ^> ^args)
{
fstream myFile ("dictionary.bin", ios::in | ios::out | ios::binary);
Word w;
w.theword = "zanzibar";
w.ID = 1;
myFile.write ((char*)&w, sizeof (Word));
myFile.close();

fstream myFile2 ("dictionary.bin", ios::in | ios::out | ios::binary);
Word w2;
myFile2.read ((char*)&w2, sizeof (Word));
myFile2.close();
std::cout << w2.theword;

}
Of course it doesn't work. You can't just cast a class pointer to a char*, where did you get that idea from? You'd have to think of a file format to store Words and then write save/load methods for your class.
Last edited on
Topic archived. No new replies allowed.