944,076 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2687
  • C++ RSS
Apr 19th, 2007
0

Read data from a two-way array from a .DAT file?

Expand Post »
This assignment I must read this .DAT file the instructor has given to me, and print out the information from it.

The .DAT file is for a two-way array
It has 6 rows and 8 colloumns

well I have ran into a couple of problems

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <iomanip>
  6. #include <string>
  7. using std::endl;
  8. using std::setw;
  9. #define clrscr() system("cls")
  10. struct noiseLevel
  11. {
  12. short vehicleNo;
  13. short noise[8];
  14. };
  15. void printArray(int[]);
  16. noiseLevel noise;
  17. int myCarArray[6][8];
  18. int main(void)
  19. {
  20. int noise;
  21. int row;
  22. int col;
  23. clrscr();
  24. ifstream noiseFile ( "a:\\noise.dat", ios::in | ios::binary | ios::nocreate );
  25. if (!noiseFile )
  26. {
  27. cout << "\nUnable to open NOISE.DAT";
  28. cout << "\nPress any key to continue.....";
  29. cin.get();
  30. return 1;
  31. }
  32.  
  33. /* Read and fill the array */
  34. for (row = 0; row < 6; row++)
  35. {
  36. noiseFile.read ( reinterpret_cast<char *> (&noise), sizeof (noise));
  37.  
  38. for(col = 0; col < 8; col++)
  39. {
  40. myCarArray[row][col+1]=noise.noiseFile[col]; // left of .noiseFile must have class/struct/union type
  41. }
  42. /* print it all out */
  43. for (row = 0; row < 6; row++)
  44. {
  45. noiseFile.read ( reinterpret_cast<char *> (&noise), sizeof (noise));
  46.  
  47. for(col = 0; col < 6; col++)
  48. {
  49. cout << setw(5) << myCarArray[row][col]; // '<<': no operator defined which takes a right hand operand of type
  50. cout << endl;
  51. }

I have gotten two errors

I put the errors in comments next to code

Any help appreciated
Last edited by youngone; Apr 19th, 2007 at 11:09 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007
Apr 19th, 2007
0

Re: Read data from a two-way array from a .DAT file?

Perhaps you meant to declare noise as a noiseLevel? Then, the only thing wrong with this statement would be that you would need to use noise.noise, not noise.noiseFile:
C++ Syntax (Toggle Plain Text)
  1. myCarArray[row][col+1]=noise.noiseFile[col];
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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: c++ with graphics
Next Thread in C++ Forum Timeline: Hi





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


Follow us on Twitter


© 2011 DaniWeb® LLC