Hello, I am trying to write data to a binary file. The function runs (the cout << "TEST" shows me that my loop is running the correct amount of times), but after the program comes to the end, it crashes and doesn't save the data to the binary file. Can anybody see what I am doing wrong?? Any tips/ nudges in the right direction would be appreciated. Thanks!

void Save_Data(int seats[][NUM_SEATS_PER_ROW])
{
     
  int row;
  char seatLetter;
  seats[NUM_ROWS][NUM_SEATS_PER_ROW];
  enum seatStatus {empty, reserved, purchased};
  seatStatus status;
  
  fstream binFile;
  binFile.open ("PLANE.bin", ios::binary | ios::out | ios::in);
  

  for (row = 0; row <= NUM_ROWS; row ++)
  {
      for (seatLetter = 'A'; seatLetter <= 'F'; seatLetter++)
      {
         if (seats[row][seatLetter] != empty)
         { 
         binFile.write(reinterpret_cast<const char*>(row), sizeof(int));
         binFile.write(reinterpret_cast<const char*>(seatLetter), sizeof(char));
         binFile.write(reinterpret_cast<const char*>(&seats[row][seatLetter]), sizeof
                                                                        (seatStatus));

         }
      } 
  }

  
  binFile.close();

} // End of function

you have defined two variables with the same name -- seats, one is a parameter and the other a local variable. My guess is that you ignored your compiler's warning about that. NEVER ignore warnings because most of them are errors.

Hello,

Well, I wasn't defining "seats" again as a local variable. I was just setting the size of the array (I thought that would be ok since I left out the 'int' before the variable inside the function). The complier doesn't give me any warnings about this....I tried commenting it out though and get the same results as before.

Terri

It's not a re-declaration, just a useless out-of-bound reference to an element of the array.

> for (row = 0; row <= NUM_ROWS; row ++)
This too steps off the end of the array.

> for (seatLetter = 'A'; seatLetter <= 'F'; seatLetter++)
And how to the upper case letters relate to NUM_SEATS_PER_ROW?
Unless your array is padded with lots of dead space, using these characters to index the array is yet more out of bound access.

> reinterpret_cast<const char*>(row)
Should probably be &row

> (&seats[row][seatLetter]), sizeof(seatStatus)
But your array is an array of ints, not an array of enums. What if they're different sizes (which C++ allows them to be).

Hello,

The uppercase 'A' to 'F' relate to the seats per row (there are 6 seats per row).

I tried placing & before row....the PLANE.BIN file is now created....I just have to get the program to read it now.

Thanks for your help!

Terri

Ok, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.