Working with Binary Files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Working with Binary Files

 
0
  #1
May 6th, 2009
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.
  1. // emprecs.cpp
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. const int SIZE = 12;
  8. struct employee {
  9. char fname[SIZE];
  10. char lname[SIZE];
  11. float wage;
  12. float hours;
  13. void print() ;
  14. };
  15. // readTxt() reads the records in input.txt, stores them in ary,
  16. // and returns the number of records read
  17. int readTxt(employee ary[]);
  18. // writeBin() writes the array of employee records to output.bin
  19. void writeBin(employee ary[], int n);
  20.  
  21. void readFile(employee ray[]);
  22.  
  23. int main()
  24. {
  25. employee payroll[SIZE];
  26.  
  27. int count = readTxt(payroll); // count = number of records in file
  28. if ( count > 0 ) {
  29. writeBin(payroll, count);
  30. } // endif
  31.  
  32. readFile(payroll) ;
  33. cout << endl;
  34. cout.write(reinterpret_cast<char *>(payroll), sizeof(employee) * count);
  35.  
  36. cout << "\n\n-- Program Complete --\n\n" ;
  37.  
  38. return 0;
  39. }
  40.  
  41. int readTxt(employee ary[])
  42. {
  43. char temp[SIZE];
  44. int k = 0;
  45. ifstream inFil( "input.txt" );
  46.  
  47. if ( inFil.is_open() ) {
  48. while ( !inFil.eof() ) {
  49. inFil.getline(ary[k].fname, SIZE, ',');
  50. inFil.getline(ary[k].lname, SIZE, ',');
  51. inFil.getline(temp, SIZE, ',');
  52. ary[k].wage = static_cast<float>(atof(temp));
  53. inFil.getline(temp, SIZE);
  54. ary[k].hours = static_cast<float>(atof(temp));
  55. k++;
  56. } // endwhile
  57. for ( int j = 0; j < k-1; j++ )
  58. cout << ary[j].fname << ',' << ary[j].lname << ','
  59. << ary[j].wage << ',' << ary[j].hours << endl;
  60. inFil.close();
  61. }
  62. else {
  63. cout << "\n\nUnable to open input file. Please verify filename and permissions.\n\n";
  64. } // endif
  65. return k;
  66. }
  67.  
  68. void writeBin(employee ary[], int n)
  69. {
  70. ofstream binOut("output.bin", ios::binary);
  71.  
  72. if ( binOut.is_open() ) {
  73. binOut.write(reinterpret_cast<char *>(&n), sizeof(int));
  74. binOut.write(reinterpret_cast<char *>(ary), sizeof(employee) * n);
  75. binOut.close();
  76. }
  77. else {
  78. cout << "\nCannot open output file - verify filename and permissions\n";
  79. } // endif
  80. }
  81.  
  82. void readFile(employee ray[])
  83. {
  84. ifstream fin("output.bin", ios::in | ios::binary);
  85. if ( fin.is_open() ) {
  86. fin.read( reinterpret_cast <char *>(ray), sizeof(employee) * SIZE);
  87. cout << "Number of bytes read is: " << fin.gcount();
  88. fin.close();
  89. }
  90. else
  91. cout << "File not opened" << endl;
  92. } // endif
  93.  
  94. /*
  95. Example Output:
  96. Martha, Stewart,2.4,40
  97. Jim, Jailson,3.4,30
  98. George, Jefferson,5.9,45
  99. Number of bytes read is: 132
  100. ♦ Martha cÉδ% Stewart FB ÜÖ↓@ BJim ☻ ¶δ% Jailson íRcÜÖY@ ≡AGeorge `C☼
  101.  Jefferson ═╠╝@ 4B δ% ☻╡ScY┼☼ `C☼
  102.  
  103. -- Program Complete --
  104.  
  105. Press any key to continue . . .
  106. */
  107.  
  108. /*
  109.  */
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Working with Binary Files

 
0
  #2
May 6th, 2009
line 73 writes out the number of array elements. So line 86 has to read it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,674
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Working with Binary Files

 
0
  #3
May 6th, 2009
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
  1. cout.write(reinterpret_cast<char *>(payroll), sizeof(employee) * count);
Looks odd - do you really want to put the memory image to screen? cout is a text based stream.
"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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC