Using C Hints Use binary file readwrite to store employee s
Using C++...
Hints
- Use binary file read/write to store employee structures effeciently
- You can find the size of struct Employee using sizeof(Employee).
- Structs can be written out to a file using write() function.
- write() function takes address to a memory and the number of bytes to be written to the file.
It may be easier to get first and last name using std::string as follows:
- You can easily convert a char[] to a string as follows
- It is possble to get a const char[] from an std::string using c_str() function.
Step 1 Create a struct that can store employee id, wages and first and last names. To keep things simple, we assume that employee names are stored in charll, and we only store the first 16 characters of the name. See below. struct Employee char fname [16] char Iname [16] int id double wages Step 2 write a program that adds an employee to the employees at file. add-employee Enter firstname Captain Enter lastname: America. Enter id: 2 Enter wage: 5.00 This program will append this record to the employees dat Step 3 write a program that prints the info of the employee with the passed employee id. s prn-employee 2 America, Captain id wage $5.00Solution
as C++ is known as powerful lanuage because of classess (superior in comparison with struct) so class employee is created.
#include <fstream.h>
class Employee
{
int id;
char fname[16];
char lname[16];
double wages;
public:
//To display record based on employee id
void diplay(int );
//To append the data of employee
void append();
};
void Empoyee::append()
{
fstream File;
File.open( “EMP.DAT”, ios::binary | ios::in | ios::out);
cin>>id;
cin.getline(fname, 20);
cin.getline(lname, 20);
cin>>wages;
File.write( (char*)this, sizeof(Employee));
File.close();
}
void Employee::Search(int data)
{
fstream File;
File.open( “EMP.DAT”, ios::binary | ios::in);
File.read((char*)this, sizeof(Employee));
cout << id<< ”==> ” << Employee<< endl;
File.close();
}
Still if you need structure , here is the code
#include <vector>
#include <string.h>
using namespace std;
typedef struct employee
{
char fname[16];
char lname[16];
int id;
double wages
}emp;
int main()
{
//values are hard coded
emp apprentice[3];
strcpy(apprentice[0].fname, \"john\");
strcpy(apprentice[0].fname, \"sina\");
apprentice[0].id = 21;
apprentice[0].wages=2000;
strcpy(apprentice[1].fname, \"under\");
strcpy(apprentice[1].fname, \"taker\");
apprentice[1].id = 20;
apprentice[1].wages=3000;
strcpy(apprentice[2].fname, \"under\");
strcpy(apprentice[2].fname, \"taker\");
apprentice[2].id = 20;
apprentice[2].wages=3000;
// Serializing struct to employee.data
ofstream output_file(\"employee.data\", ios::binary);
output_file.write((char*)&apprentice, sizeof(apprentice));
output_file.close();
// Reading from it
ifstream input_file(\"employee.data\", ios::binary);
emp master[3];
input_file.read((char*)&master, sizeof(master));
for (emp e = 0; e < 3; e++)
{
cout << \"Id #\" << e << endl;
cout << \"fname: \" << master[e].fname << endl;
cout << \"lname: \" << master[e].lname << endl;
cout << \"wages: \" <<master[e].wages<< endl;
cout << endl << endl;
}
return 0;
}


