Thursday, 30 April 2020

Write a program to maintain a elementary database of employees using files.

#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iomanip.h>
class group
{
private:
struct person
{
char flag;
char empcode[ 5 ];
char name[ 40 ];
int age;
float sal;
} p;
fstream file;
public:
group( );
void addrec( );
void listrec( );
void modirec( );
void delrec( );
void recallrec( );
void packrec( );
void exit( );
};
void main( )
{
char choice;
group g;
do
{
clrscr( );
gotoxy( 30, 10 );
cout << "1. Add records";
gotoxy( 30, 11 );
cout << "2. List records";
gotoxy( 30, 12 );
cout << "3. Modify records";
gotoxy( 30, 13 );
cout <<"4. Delete records";
gotoxy( 30, 14 );
cout << "5. Recall records";
gotoxy( 30, 15 );
cout << "6. Pack records";
gotoxy( 30, 16 );
cout << "0. Exit";
gotoxy( 30, 18 );
cout << "Your Choice ? ";
cin >> choice;
clrscr( );
switch( choice )
{
case '1':
g.addrec( );
break;
case '2':
g.listrec( );
break;
case '3':
g.modirec( );
break;
case '4':
g.delrec( );
break;
case '5':
g.recallrec( );
break;
case '6':
g.packrec( );
break;
case '0':
g.exit( );
break;
}
}while( choice != 0 );
}
void group::group( )
{
file.open( "emp.dat", ios::binary || ios::in || ios::out );
if( !file )
{
cout << endl << "Unable to open file";
exit( );
}
}
void group::addrec( )
{
char ch;
file.seekp( 0L, ios::end );
do
{
cout << endl << "Enter emp code, name, age & salary" << endl;
cin >> p.empcode >> p.name >> p.age >> p.sal;
p.flag = '';
file.write( ( char * )&p, sizeof( p ) );
cout << "Add another record? (Y/N)";
cin >> ch;
} while( ch == 'Y' || ch == 'Y' );
}
void group::listrec( )
{
int j = 0,a;
file.seekg( 0L, ios::beg );
while( file.read( ( char * )&p, sizeof( p ) ) )
{
if( p.flag != '*' )
{
cout <<endl << "Record#" << j++ << setw( 6 )<< p.empcode<<setw(20)<<p.name<<setw(4<<p.age<<setw(9)<< p.sal ;
}
file.clear( );
cout << endl << "Press any key......";
getch( );
}
}
void group::modirec( )
{
char code[ 5 ];
int count = 0;
long int pos;
cout << "Enter employee code: ";
cin >> code;
file.seekg( 0L, ios::beg );
while( file.read( ( char * )&p, sizeof( p ) ) )
{
if( strcmp( p.empcode, code ) == 0 )
{
cout << endl << "Enter new record" << endl;
cin >> p.empcode >> p.name >> p.age;
p.flag = '';
pos = count * sizeof( p );
file.seekp( pos, ios::beg );
file.write( ( char * )&p, sizeof( p ) );
return;
}
count++;
}
cout << endl << "No employee in file with code = " << code;
cout << endl << "Press any key .....";
getch( );
file.clear( );
}
void group::delrec( )
{
char code[ 5 ];
long int pos;
int count = 0;
cout << "Enter employee code : ";
cin >> code;
file.seekg( 0L, ios::beg );
while( file.read( ( char * )&p, sizeof( p ) ) )
{
if( strcmp( p.empcode, code ) == 0 )
{
p.flag = '*';
pos = count * sizeof( p );
file.seekp( pos, ios::beg );
file.write( ( char * )&p, sizeof( p ) );
return;
}
count++;
}
cout << endl << "No employee in file with code = " << code;
cout<<endl<<Press any key ....";
getch( );
file.clear( );
}
void group.recallrec()
{
char code[ 5 ];
long int pos;
int count = 0;
cout << "Enter employee code: ";
cin >> code;
file.seekg( 0L, ios::beg );
while( file.read( ( char * )&p, sizeof( p ) ) )
{
if( strcmp( p.empcode, code ) == 0 )
{
p.flag = '';
pos = count * sizeof( p );
file.seekp( pos, ios::beg );
file.write( ( char * )&p, sizeof( p ) );
return;
}
count++;
}
cout << endl << "No employee in file with code = " << code;
cout << endl << "Press any key ....";
file.clear( );
}
void group::packrec( )
{
ofstream outfile;
outfile.open( "TEMP", ios::out );
file.seekg( 0, ios::beg );
while( file.read( ( char * )&p, sizeof( p ) ) )
{
if( p.flag != '*' )
outfile.write((char *)&p,sizeof(p)));
}
outfile.close( );
file.close( );
remove( "EMP.dat" );
rename( "TEMP", "TEMP.dat" );
file.open( "EMP.dat", ios::binary | ios::in | ios::out | ios::nocreate );
}
void group::exit( )
{
file.close( );
}

No comments:

Post a Comment