Sunday, August 2, 2009

Structures and binary I/O in C++?

I'm in an Intro to C++ class and have been trying to debug my program for hours now. I'm supposed to read in a file that has account information for multiple bank accounts. So, I have the following structure that corresponds to the format of the file:





struct CreditAccount


{


char acctNum[20];


char lastName[21];


char firstName[21];


int expMon;


int expYr;


double credLim;


double bal;


};





Then, I need to take what's in the structure and write it to another .dat file to be used in another program.





In main(), I have:





CreditAccount credAcct;


ifstream inFile;


ofstream outFile;





inFile.open("accountinfo. txt", ios::binary);


if(inFile.fail()) ...


outFile.open("accounts", ios::binary);


if(outFile.fail()) ...





... I'm running out of space. I'll add the rest to additional details in a minute ...

Structures and binary I/O in C++?
Get rid of the "%26gt;%26gt;" reads and "%26lt;%26lt;" writes





inFile %26gt;%26gt; credAcct.expMon;


outFile %26lt;%26lt; credAcct.expMon;





and rewrite it like you did the char strings. E.g.





inFile.read((char*) %26amp;credAcct.expMon, sizeof(credAcct.expMon));





outFile.write((char *) %26amp;credAcct.expMon, sizeof(credAcct.expMon));





Make any difference? Why?
Reply:You've gone through the trouble of showing us all sorts of code and then tell us it works, but it is handling the data incorrectly. *How* is it handling it incorrectly? What is the problem? I'm not even going to try looking through your code unless you give me more to go on than that.





If you want to try debugging this yourself, start printing some debug messages to your screen :


fprintf(stdout, "...");


so you can maybe figure this out for yourself.


No comments:

Post a Comment