cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
.bin file i/o troubles
.bin file i/o troubles
Mar 8, 2011 at 10:43pm UTC
frasiercrane
(3)
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;
}
Mar 9, 2011 at 12:11am UTC
hanst99
(2869)
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
Word
s and then write save/load methods for your class.
Last edited on
Mar 9, 2011 at 12:13am UTC
Topic archived. No new replies allowed.