First, I'm supposed to open an binary file, read the first piece of information (number of structures), and than dynamically allocate an array of structures, and read in the rest into the array of structure. It seems that I have achieved that, but when I try to print the structures (ex/ employee[0].firstname) anywhere besides where I read in the file, it does not print any information. It just prints blanks. I'm not quite sure what I am doing incorrectly, any tips?
Secondly I have the line commented out in the main function of deleting the allocated memory, because when I run the program it triggers a breakpoint, what am I doing incorrectly there, how do I properly delete that array of structures?
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cout;
using std::ifstream;
using std::ios;
using std::setw;
using std::endl;
using std::left;
struct Emp //employee strcture given in the problem
{
char firstname[20];
char lastname[20];
float wage;
float hours;
}employee[15]; //create an array of employees for the structure
int ReadFile();
void PrintInfo(Emp * employee, int numRecords);
int main() {
int numRecords = 0; //variable to store the number of records
//call function to read in file
numRecords = ReadFile();
//call function to print info on screen
PrintInfo(employee, numRecords);
//delete[] employee;//deallocate memory
system("pause");
return 0;
}//end main
int ReadFile()
{
int numRecords = 0;//variable to store number of records in file
ifstream data("record.dat", ios::in | ios::binary); //open binary file
if (data.is_open())//verify binary file is open
{
data.read(reinterpret_cast<char*>(&numRecords), sizeof(int)); //store number of records into variable
Emp *employee;
employee = new Emp[numRecords]; //create a dynamic array to accomodate data (from num of records variable)
while (!data.eof())
{
for (int i = 0; i < numRecords; i++)//loop to read in stuctures
{
data.read ( reinterpret_cast<char*> (&employee[i]), sizeof(Emp));//read employees info in from binary file into dynamic array
}//end for loop
}
data.close();//close binary file
}//end of if open
else
{
cout << "ERROR: Cannot open file.";//print out error if file is not open
}
cout << *employee[0].firstname;
return numRecords; //return number of records
}// end ReadFile
void PrintInfo(Emp *employee, int numRecords)
{
//header for screen
cout << left << setw(30) << "Name" << setw(10) << "Wage" << setw(5) << "Hours Worked" << endl;
//print employees info on screen
for (int i = 0; i < numRecords; i++)//for loop to print records
{
cout << left << setw(30) << employee[i].firstname << " " << employee[i].lastname
<< setw(10) << employee[i].wage << setw(5) << employee[i].hours << endl;
}//end for loop to print info on screen
}//end PrintInfo
While it is not the best, I have got around my issue. I was able to call my PrintInfo function from the ReadFile function. Reformatted some of the outputs also.
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cout;
using std::ifstream;
using std::ios;
using std::setw;
using std::endl;
using std::left;
struct Emp //employee strcture given in the problem
{
char firstname[20];
char lastname[20];
float wage;
float hours;
}; //create an array of employees for the structure
void ReadFile();
void PrintInfo(Emp * employee, int numRecords);
int main() {
int numRecords = 0; //variable to store the number of records
//call function to read in file
ReadFile();
system("pause");
return 0;
}//end main
void ReadFile()
{
int numRecords = 0;//variable to store number of records in file
ifstream data("record.dat", ios::in | ios::binary); //open binary file
if (data.is_open())//verify binary file is open
{
data.read(reinterpret_cast<char*>(&numRecords), sizeof(int)); //store number of records into variable
Emp *employee;
employee = new Emp[numRecords]; //create a dynamic array to accomodate data (from num of records variable)
while (!data.eof())
{
for (int i = 0; i < numRecords; i++)//loop to read in stuctures
{
data.read ( reinterpret_cast<char*> (&employee[i]), sizeof(Emp));//read employees info in from binary file into dynamic array
}//end for loop
}
data.close();//close binary file
//call function to print info on screen
PrintInfo(employee, numRecords);
delete[] employee;//deallocate memory
}//end of if open
else//print out error if file is not open
{
cout << "ERROR: Cannot open file.";
}//end else
}// end ReadFile
void PrintInfo(Emp *employee, int numRecords)
{
//header for screen
cout << left << setw(20) << "First Name" << setw(20) << left << "Last Name" << left << setw(10) << left << "Wage" << left << setw(5) << "Hours Worked" << endl;
//print employees info on screen
for (int i = 0; i < numRecords; i++)//for loop to print records
{
cout << left << setw(20) << employee[i].firstname << setw(20)<< left << employee[i].lastname
<< setw(15) << employee[i].wage << setw(10) << employee[i].hours << endl;
}//end for loop to print info on screen
}//end PrintInfo