Two lines:
outf.write( reinterpret_cast<char *>( &temp ), sizeof( Item ) );
and
inf.read( reinterpret_cast<char *>( &temp ), sizeof( Item ) );
wrong. You write a binary object layout, including instances of std::string . This means that you write the value of pointers to a file and read them back.
You cannot just read pointers from a file and assume that they point to real memory, especially if it was stored by a temporary instance of std::string , which was supposed to free the memory in it with the destructor when it went out of scope. I am surprised that you got this to run “correctly” with any compiler.
Your program should write the content and read it using the operator<< and operator>> methods. It should look like this:
void write_to_file( const string& fn, const Item& item ) { fstream outf( fn.c_str(), ios::binary | ios::out ); outf << item << std::endl; outf.close(); } void read_from_file( const string& fn, Item& item ) { fstream inf( fn.c_str(), ios::binary | ios::in ); if( !inf ) { cout << "What wrong?"; } inf >> item; inf.close(); }
BONUS : There are a few quirks with your code.
This statement, fortunately, is not currently used in your program (the method is not called).
return ( string& )"";
Not valid because you will return a link to a temporary string object. Remember that the string literal "" not a std::string object, and you cannot get a link to it of the std::string& . You should probably throw an exception, but you could avoid:
string& operator []( int x ) { static string unknown; if ( 0 == x ) return itemID; if ( 1 == x ) return itemName; if ( 2 == x ) return itemState; return unkonwn; }
This is a bad decision, given that the string is returned by reference. It can be changed by the caller, so it may not always return the value "" , which, in his opinion, would be. However, it will remove the undefined behavior for your program.
Calling the .close() method for std::fstream objects is not required; the destructor automatically calls it when the object goes out of scope. Inserting a call causes additional clutter.
Also, what's the redundant naming convention?
class Item { private: string itemID; string itemName; string itemState;
What is wrong with their call to ID , name and state ?