Sorry but I am going to disagree with Freaky_Chris for the second time in two days! I hope he realizes the I agree with him most of the time :)
Anyway, you MAJOR sin is to think that reading into a class which may or may-not allocate memory will work. Let us take an example:
class X
{
int * array;
};
Now the size of class is undetermined. You don't know when the size will change. So NEVER do a binary read into a class/struct unless you have absolute certainty about the size. Practically that means no pointers to memory, no STL object (e.g. vector or string) since these reserve an unknown amount of memory, no classes with static objects.
In your case you read from User. And look it has std::string. And wait, std::string often uses reference counting and static members, so that you corrupt memory is no surprise.
Your best bet is to read each item one by one. e.g.
void write(std::ostream& OutS) const
{
OutS<<username<<std::endl;
OutS<<password<<std::endl;
OutS<<verified<<std::endl;
return;
}
void
User::read(std::istream& Input)
{
Input.getline(username,'\n');
Input.getline(password,'\n');
Input>>isVerified;
return;
}
That will actually give you LESS bytes of output than a binary dump.
p.s. you had better add some error checking to the read method, and you could implement some operator<< and operator>> methods if you like.
[Note: To the experts: I know you can do the binary writing and reading but that is because you know exactly how to do it on a case by case basis. Everyone else say well clear.]
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
Whoa, I would NEVER have thought of that string issue if you wouldn't have told me. I'll look into it but just a few questions while we're at it:
1) How do you explain that it works when I first write to the file and then read,
Well that is not an easy question since my version of your code doesn't. BUT if when you read/write, you look like you may have caused string to reserve the same length. It is also possible that
you are writing junk to a piece of unused memory. (not that string is referenced counted, so you should check that changing the original AFTER your read/write doesn't change the string on the later -- that would be another unexpected corruption.
Note you may get different behaviour dependent on you compiler optimization flags, your compiler choice and which implementation of the STL you choose.2) With the "<<" thingies, you're suggesting I turn my binary file into a text file, don't you?
Yes. But how can you tell the difference (AFTER it has been written)
of a binary file with the characters
"abc" or a text file with the characters "abc". :)3) Can't the string issue be solved by using char arrays of predetermined size?
Yes -- Careful because that sort of thing is very easy to get wrong in different ways.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
I am glad that solved the problem.
The best thing to do is mark this thread as solved.
Then ask the question again in a new thread.
(I am really not the person to answer your questions, at lunch at work, my group were trying to figure out how many hours we had used each OS, I got to 50 for all MS systems in total, compared to 15000 for Linux. I have had a cushy life :) )
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
Apparently the file/program, whatever is located on the D drive in your computer, not the C. It may be that you've partitioned your drive or you have a external drive or something else. You should be able to go someplace to look at the drive contents in the computer. I don't have Vista so the details aren't know to me for sure. However in the two Windows versions I have at the moment you can go Start->Accessories->Windows Explorer to see a tree of the drives and click on the appropriate drive letter to see what's in there. You can get the same information with a slightly different display arrangement by going Start->MyComputer and clicking on the appropriate drive letter.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396