I have a problem displaying the contents of a file, and I need some input on it, I'll keep everything brief and well organized:
I'm creating records with structures, this is the structure definition:
1 2 3 4 5 6 7 8 9
|
const int NAME_SIZE = 6;
struct Company
{
char name[NAME_SIZE];
int quarter[4];
double quarterSales[4];
};
|
and these are the created structures:
1 2 3 4
|
Company northDivision;
Company eastDivision;
Company southDivision;
Company westDivision;
|
I have populated these structures with data, and written that data to a file that is opened for output in binary mode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
File.open("CompanySales.dat", ios::out | ios::binary);
//write north division data to file
File.write(reinterpret_cast<char *>(&northDivision), sizeof(northDivision));
//write east division data to file
File.write(reinterpret_cast<char *>(&eastDivision), sizeof(eastDivision));
//write south division data to file
File.write(reinterpret_cast<char *>(&southDivision), sizeof(southDivision));
//write west division data to file
File.write(reinterpret_cast<char *>(&westDivision), sizeof(westDivision));
File.close();
|
The file is created and there is data in it (about 224 bytes or so)
and then I open the file for input in binary mode as well as create a new Company structure to hold the data being read into memory from the file:
1 2 3
|
Company division; // use for displaying file
File.read(reinterpret_cast<char *>(&division), sizeof(division));
|
Now here is where things went wrong, when the program encounters this while loop, the program just hangs, it doesn't fail, it never even reaches the end of the file, it just sits there:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//while not at end of file, display records
while(!File.eof());
{
//Display the record.
cout << division.name << " Division" << endl;
cout << "Sales for Quarter " << division.quarter[0] << ":";
cout << "$" << division.quarterSales[0] << endl;
cout << "Sales for Quarter " << division.quarter[1] << ":";
cout << "$" << division.quarterSales[1] << endl;
cout << "Sales for Quarter " << division.quarter[2] << ":";
cout << "$" << division.quarterSales[2] << endl;
cout << "Sales for Quarter " << division.quarter[3] << ":";
cout << "$" << division.quarterSales[3] << endl;
cout << endl << endl;
//read the next record
File.read(reinterpret_cast<char *>(&division), sizeof(division));
}
|
All this code is built off of examples from my programming book and I can't seem to get any more info from it, what mistakes have I made? Any help would mean a world of thanks from me.
Thanks in advance for any help anyone can offer.