B
bintom
I have the following File Handling program, wherein the modify method
is not modifying the concerned record, but is added the modified
record to the end of the file.
Also, the size of employee class is showing as 44, when it should be
37.
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
struct date
{ int day, month, year; };
class employee
{ private: char flag, empcode[5], name[21];
date doj;
float sal;
public : void add()
{ char to_write;
flag=' ';
cout << "Enter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
if(toupper(to_write) == 'Y')
diskout();
}
void diskout()
{ ofstream outfile("Group.txt", ios::app);
outfile.write((char *)this, sizeof(*this));
}
void diskin()
{ ifstream infile("Group.txt");
cout << "CODE NAME Join Date
SALARY\n";
cout << "==== ==== =========
======\n\n";
while(infile.read((char *)this, sizeof(*this)))
output();
getch();
}
void output()
{ cout.setf(ios::left);
cout << setw(7) << empcode << setw(21) << name << " ";
(doj.day < 10)? cout << "0": cout << ""; cout << doj.day
<< "/";
(doj.month < 10)? cout << "0": cout << "" << "";
cout << doj.month << "/" << doj.year;
cout.unsetf(ios::left);
cout.setf(ios::showpoint);
cout << setw(10) << sal << "\n";
}
void modify()
{ char code[5], to_write; int count=0; long pos;
cout << "Enter employee code to be modified: ";
cin >> code;
fstream file("Group.txt", ios::ate | ios::in | ios:
ut);
file.seekg(0);
while(file.read((char *)this, sizeof(*this)))
{ if(strcmp(empcode, code) == 0)
{ output();
cout << "\nEnter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name,
21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
pos = count*sizeof(employee);
file.seekp(pos, ios::beg);
cout << "\n\n" << pos << "\n\n";
getch();
if(toupper(to_write) == 'Y')
diskout();
break;
}
count++;
}
}
};
int main()
{ char choice;
employee e;
cout << "Sizeof employee = " << sizeof(employee);
getch();
do { system("Cls");
cout << "EMPLOYEE DATABASE MANAGEMENT\n\n";
cout << "1. Add a record\n";
cout << "2. Sort and List all records\n";
cout << "3. Modify a record\n";
cout << "0. Exit\n\n";
cout << "Enter your choice: ";
cin >> choice;
system("Cls");
switch(choice)
{ case '1': e.add(); break;
case '2': e.diskin(); break;
case '3': e.modify(); break;
}
} while(choice != '0');
return 0;
}
is not modifying the concerned record, but is added the modified
record to the end of the file.
Also, the size of employee class is showing as 44, when it should be
37.
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
struct date
{ int day, month, year; };
class employee
{ private: char flag, empcode[5], name[21];
date doj;
float sal;
public : void add()
{ char to_write;
flag=' ';
cout << "Enter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
if(toupper(to_write) == 'Y')
diskout();
}
void diskout()
{ ofstream outfile("Group.txt", ios::app);
outfile.write((char *)this, sizeof(*this));
}
void diskin()
{ ifstream infile("Group.txt");
cout << "CODE NAME Join Date
SALARY\n";
cout << "==== ==== =========
======\n\n";
while(infile.read((char *)this, sizeof(*this)))
output();
getch();
}
void output()
{ cout.setf(ios::left);
cout << setw(7) << empcode << setw(21) << name << " ";
(doj.day < 10)? cout << "0": cout << ""; cout << doj.day
<< "/";
(doj.month < 10)? cout << "0": cout << "" << "";
cout << doj.month << "/" << doj.year;
cout.unsetf(ios::left);
cout.setf(ios::showpoint);
cout << setw(10) << sal << "\n";
}
void modify()
{ char code[5], to_write; int count=0; long pos;
cout << "Enter employee code to be modified: ";
cin >> code;
fstream file("Group.txt", ios::ate | ios::in | ios:
file.seekg(0);
while(file.read((char *)this, sizeof(*this)))
{ if(strcmp(empcode, code) == 0)
{ output();
cout << "\nEnter empcode: "; cin >> empcode;
cin.ignore(1, '\n');
cout << "Enter name: "; cin.getline(name,
21);
cout << "Enter join year: "; cin >> doj.year;
cout << "Enter join month: "; cin >> doj.month;
cout << "Enter join day: "; cin >> doj.day;
cout << "Enter salary: "; cin >> sal;
cout << "\nWrite to disk? "; cin >> to_write;
pos = count*sizeof(employee);
file.seekp(pos, ios::beg);
cout << "\n\n" << pos << "\n\n";
getch();
if(toupper(to_write) == 'Y')
diskout();
break;
}
count++;
}
}
};
int main()
{ char choice;
employee e;
cout << "Sizeof employee = " << sizeof(employee);
getch();
do { system("Cls");
cout << "EMPLOYEE DATABASE MANAGEMENT\n\n";
cout << "1. Add a record\n";
cout << "2. Sort and List all records\n";
cout << "3. Modify a record\n";
cout << "0. Exit\n\n";
cout << "Enter your choice: ";
cin >> choice;
system("Cls");
switch(choice)
{ case '1': e.add(); break;
case '2': e.diskin(); break;
case '3': e.modify(); break;
}
} while(choice != '0');
return 0;
}