| | |
Working with Binary Files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
So my Data Structures class has finished the book we were using, but we still have a week of school left. My instructor has started going over binary files... only problem is I wasn't there when he went over it.
My assignment is to open a binary file, save the contents to a dynamic array, and output the contents to the screen. I'm getting close (I think) but when I output the array, it's not outputting in clear text. What am I doing wrong?
Here's what I have so far... but I'm really confused right now. This is the accumulation of a lot of copy/paste.
My assignment is to open a binary file, save the contents to a dynamic array, and output the contents to the screen. I'm getting close (I think) but when I output the array, it's not outputting in clear text. What am I doing wrong?
Here's what I have so far... but I'm really confused right now. This is the accumulation of a lot of copy/paste.
c++ Syntax (Toggle Plain Text)
// emprecs.cpp #include <iostream> #include <fstream> using namespace std; const int SIZE = 12; struct employee { char fname[SIZE]; char lname[SIZE]; float wage; float hours; void print() ; }; // readTxt() reads the records in input.txt, stores them in ary, // and returns the number of records read int readTxt(employee ary[]); // writeBin() writes the array of employee records to output.bin void writeBin(employee ary[], int n); void readFile(employee ray[]); int main() { employee payroll[SIZE]; int count = readTxt(payroll); // count = number of records in file if ( count > 0 ) { writeBin(payroll, count); } // endif readFile(payroll) ; cout << endl; cout.write(reinterpret_cast<char *>(payroll), sizeof(employee) * count); cout << "\n\n-- Program Complete --\n\n" ; return 0; } int readTxt(employee ary[]) { char temp[SIZE]; int k = 0; ifstream inFil( "input.txt" ); if ( inFil.is_open() ) { while ( !inFil.eof() ) { inFil.getline(ary[k].fname, SIZE, ','); inFil.getline(ary[k].lname, SIZE, ','); inFil.getline(temp, SIZE, ','); ary[k].wage = static_cast<float>(atof(temp)); inFil.getline(temp, SIZE); ary[k].hours = static_cast<float>(atof(temp)); k++; } // endwhile for ( int j = 0; j < k-1; j++ ) cout << ary[j].fname << ',' << ary[j].lname << ',' << ary[j].wage << ',' << ary[j].hours << endl; inFil.close(); } else { cout << "\n\nUnable to open input file. Please verify filename and permissions.\n\n"; } // endif return k; } void writeBin(employee ary[], int n) { ofstream binOut("output.bin", ios::binary); if ( binOut.is_open() ) { binOut.write(reinterpret_cast<char *>(&n), sizeof(int)); binOut.write(reinterpret_cast<char *>(ary), sizeof(employee) * n); binOut.close(); } else { cout << "\nCannot open output file - verify filename and permissions\n"; } // endif } void readFile(employee ray[]) { ifstream fin("output.bin", ios::in | ios::binary); if ( fin.is_open() ) { fin.read( reinterpret_cast <char *>(ray), sizeof(employee) * SIZE); cout << "Number of bytes read is: " << fin.gcount(); fin.close(); } else cout << "File not opened" << endl; } // endif /* Example Output: Martha, Stewart,2.4,40 Jim, Jailson,3.4,30 George, Jefferson,5.9,45 Number of bytes read is: 132 ♦ Martha cÉδ% Stewart FB ÜÖ↓@ BJim ☻ ¶δ% Jailson íRcÜÖY@ ≡AGeorge `C☼ Jefferson ═╠╝@ 4B δ% ☻╡ScY┼☼ `C☼ -- Program Complete -- Press any key to continue . . . */ /* */
Last edited by Duki; May 6th, 2009 at 12:22 am.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
Why do your write the number of records to the binary file, but not read it it back? Instead, you're reading in the full (potential)size of the array's worth of data, which will include that one int at the front of it, putting everything else out of sync.
This line
Looks odd - do you really want to put the memory image to screen? cout is a text based stream.
This line
C++ Syntax (Toggle Plain Text)
cout.write(reinterpret_cast<char *>(payroll), sizeof(employee) * count);
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Binary Files and Seekg (C++)
- Working With Binary Files (C++)
- Binary files EOF help! (C++)
- PLEASE HELP: Doing math with 2 binary files (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: understanding statement in c++
- Next Thread: Need help in sorting
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






