Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Printing the last item of a structure twice
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jonathan Lee, post: 3910491"] Yes, but that's not actually what you want it to do. You want it to write the _value_ of rua, and the int. But that's not what it's doing. If you check sizeof(string) you will find that it is always the same regardless of the contents. On my machine, that's 4 bytes, probably because it is only a wrapper around a pointer. So when you do this: fwrite(&meu, sizeof(ender), 1, fp); you are only writing a pointer ("rua") and the int ("n"). You're not writing the string that "rua" is pointing to. Depending on your inputs, that pointer stored by "rua" might not actually change. So you write out the same pointer 3 times. Then you read that same pointer back 3 times and they all point to the same thing: the last string you've read from the keyboard. You really need a way to serialize the object (hint: Google "serialize C++") and a way to recover it. Doing it the way you have is a bad idea -- as you've seen it doesn't work. --Jonathan [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Printing the last item of a structure twice
Top