Click here to Skip to main content
15,885,767 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to get disk/drive size Pin
MKC0023-Feb-10 20:20
MKC0023-Feb-10 20:20 
GeneralRe: How to get disk/drive size Pin
Richard MacCutchan3-Feb-10 22:02
mveRichard MacCutchan3-Feb-10 22:02 
AnswerRe: How to get disk/drive size Pin
Kushagra Tiwari3-Feb-10 2:57
Kushagra Tiwari3-Feb-10 2:57 
GeneralRe: How to get disk/drive size Pin
MKC0023-Feb-10 20:19
MKC0023-Feb-10 20:19 
QuestionWriting Objects into files in binary mode Pin
Sujan Dhakal3-Feb-10 1:58
Sujan Dhakal3-Feb-10 1:58 
AnswerRe: Writing Objects into files in binary mode Pin
Cedric Moonen3-Feb-10 2:10
Cedric Moonen3-Feb-10 2:10 
GeneralRe: Writing Objects into files in binary mode Pin
Sujan Dhakal3-Feb-10 2:17
Sujan Dhakal3-Feb-10 2:17 
AnswerRe: Writing Objects into files in binary mode Pin
CPallini3-Feb-10 2:20
mveCPallini3-Feb-10 2:20 
You cannot do that.
You're trying to serialize an object (see "Serialization" topic at Wikipedia [^]) in a wrong way.
Since std::string is not a scalar type, the object instance (objdata) holds just a reference to it (and you're saving just such a reference, i.e. storing an address to disk, clearly it makes no sense).
So you cannot save the class as a whole using
file.write((char*)&objdata,sizeof(Data));

instead you have to store each of its members in order to restore appropriately them, for instance:
file.write((char*)&(objdata.id),sizeof(objdata.id));
string::size_type len = objdata.name.length();
file.write((char*) &len, sizeof(len)); // this is useful for reading back the string
file.write((char*)(objdata.name.c_str(), len); //note: not storing null terminator


Can you spot the code for reading back the class?
Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke

[My articles]

GeneralRe: Writing Objects into files in binary mode Pin
Sujan Dhakal3-Feb-10 2:28
Sujan Dhakal3-Feb-10 2:28 
QuestionUnable to paste clipboard data by sending WM_PASTE message Pin
lazy_panther3-Feb-10 1:30
lazy_panther3-Feb-10 1:30 
AnswerRe: Unable to paste clipboard data by sending WM_PASTE message Pin
Iain Clarke, Warrior Programmer3-Feb-10 3:45
Iain Clarke, Warrior Programmer3-Feb-10 3:45 
AnswerRe: Unable to paste clipboard data by sending WM_PASTE message Pin
LunaticFringe3-Feb-10 3:48
LunaticFringe3-Feb-10 3:48 
QuestionRe: Unable to paste clipboard data by sending WM_PASTE message [modified] Pin
lazy_panther3-Feb-10 16:35
lazy_panther3-Feb-10 16:35 
AnswerRe: Unable to paste clipboard data by sending WM_PASTE message Pin
LunaticFringe3-Feb-10 18:44
LunaticFringe3-Feb-10 18:44 
GeneralRe: Unable to paste clipboard data by sending WM_PASTE message Pin
lazy_panther4-Feb-10 1:50
lazy_panther4-Feb-10 1:50 
GeneralRe: Unable to paste clipboard data by sending WM_PASTE message Pin
LunaticFringe4-Feb-10 13:32
LunaticFringe4-Feb-10 13:32 
QuestionTroublesome Handling of Files in C++ Pin
Sujan Dhakal3-Feb-10 1:29
Sujan Dhakal3-Feb-10 1:29 
AnswerRe: Troublesome Handling of Files in C++ Pin
CPallini3-Feb-10 1:37
mveCPallini3-Feb-10 1:37 
AnswerRe: Troublesome Handling of Files in C++ [modified] Pin
Stuart Dootson3-Feb-10 1:46
professionalStuart Dootson3-Feb-10 1:46 
GeneralRe: Troublesome Handling of Files in C++ Pin
CPallini3-Feb-10 1:54
mveCPallini3-Feb-10 1:54 
GeneralRe: Troublesome Handling of Files in C++ Pin
Stuart Dootson3-Feb-10 2:28
professionalStuart Dootson3-Feb-10 2:28 
AnswerRe: Troublesome Handling of Files in C++ Pin
Raj Indian3-Feb-10 22:00
Raj Indian3-Feb-10 22:00 
QuestionSetUpAndDeployment Project Pin
jannathali2-Feb-10 22:31
jannathali2-Feb-10 22:31 
AnswerRe: SetUpAndDeployment Project [modified] Pin
KingsGambit2-Feb-10 23:47
KingsGambit2-Feb-10 23:47 
GeneralRe: SetUpAndDeployment Project Pin
jannathali3-Feb-10 1:03
jannathali3-Feb-10 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.