943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 901
  • C++ RSS
May 6th, 2009
0

Working with Binary Files

Expand Post »
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.
c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
May 6th, 2009
0

Re: Working with Binary Files

line 73 writes out the number of array elements. So line 86 has to read it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 6th, 2009
0

Re: Working with Binary Files

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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: understanding statement in c++
Next Thread in C++ Forum Timeline: Need help in sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC