Categories
Uncategorised

Binary Files – Employee Data

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Binary Files – Employee Data Management Program. Give user the option to create & delete file, show number of records, delete particular record, and delete
last record /

#include <fstream.h>
#include <conio.h>
#include <stdio.h>

class employee
{
int eno;
char ename[20];
double salary;
public: void getdata()
{
cout<<endl<<“Enter employee number : “;
cin>>eno;
cout<<“Enter employee name : “;
gets(ename);
cout<<“Enter employee salary : “;
cin>>salary;
}
void disp()
{
cout<<endl<<Displaying data for employee”
<<endl<<“Employee number : “<<eno
<<endl<<“Employee name : “<<ename
<<endl<<“Employee salary : “<<salary<<endl;
}
} obj1, obj2;

char ans;
fstream file, temp;
int ch, r;

void create()
{
file.open(“data”, ios::out | ios::binary);
do
{
obj1.getdata();
file.write((char
)&obj1, sizeof(obj1));
cout<<“Do you wish to enter another record? (y / n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’);
file.close();
}

void showfile()
{
file.open(“data”, ios::in);
while (file.read((char)&obj1, sizeof(obj1)))
obj1.disp();
file.close();
}

void countrec()
{
r = 0; file.open(“data”, ios::in);
while (file.read((char
)&obj1, sizeof(obj1)))
++r;
file.close();
cout<<endl<<“Number of records is “<<r<<endl;
}

void rewrite()
{
temp.open(“new”, ios::in);
file.open(“data”, ios::out);
temp.seekg(0);
while (temp.read((char)&obj2, sizeof(obj2)))
file.write((char
)&obj2, sizeof(obj2));
file.close();
temp.close();
remove(“new”);
cout<<“\nRecord you requested to be removed has been deleted\n”;
}

void delrec()
{
file.open(“data”, ios::in);
temp.open(“new”, ios::out);
r = 1;
int rec;
cout<<endl<<“Enter record number to delete : “;
cin>>rec; file.seekg(0);
while (file.read((char)&obj2, sizeof(obj2)))
{
if (r != rec)
temp.write((char
)&obj2, sizeof(obj2));
++r;
}
file.close();
temp.close();
rewrite();
}

void dellast()
{
file.open(“data”, ios::in);
temp.open(“new”, ios::out);
file.seekg(0, ios::end); // go to end of file
int lp = file.tellg();
int s = sizeof(obj1);
int lr = lp / s;
r = 1;
file.seekg(0);
while (file.read((char)&obj2, sizeof(obj2)))
{
if (r != lr)
temp.write((char
)&ob
j2, sizeof(obj2));
++r;
}
file.close();
temp.close();
rewrite();
}

void main()
{
clrscr();
do
{
cout<<endl<<“Employee Data Management Program” <<endl<<“1. Create file”
<<endl<<“2. Read file”
<<endl<<“3. Find total number of records”
<<endl<<“4. Delete nth record”
<<endl<<“5. Delete the last record”
<<endl<<“Enter your choice : “;
cin>>ch;
switch(ch)
{
case 1 : create();
break;
case 2 : showfile();
break;
case 3 : countrec();
break;
case 4 : delrec();
break;
case 5 : dellast();
break;
default : cout<<“\nYou entered an incorrect choice”;
}
cout<<“\nDo you want the menu to be shown again? (y / n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’ );
getch();
}

/* Output /

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 1

Enter employee number : 1
Enter employee name : Arthur Dent
Enter employee salary : 45000
Do you wish to enter another record? (y / n): y

Enter employee number : 2
Enter employee name : Tricia McMillian
Enter employee salary : 12000
Do you wish to enter another record? (y / n): y

Enter employee number : 3
Enter employee name : Ford Prefect
Enter employee salary : 50000
Do you wish to enter another record? (y / n): y

Enter employee number : 4
Enter employee name : Zaphod Beeblebrox
Enter employee salary : 30000
Do you wish to enter another record? (y / n): y

Enter employee number : 5
Enter employee name : Zarniwoop
Enter employee salary : 6000
Do you wish to enter another record? (y / n): n

Do you want the menu to be shown again? (y / n) : y

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 3

Number of records is 5

Do you want the menu to be shown again? (y / n) : y

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 5

Record you requested to be removed has been deleted

Do you want the menu to be shown again? (y / n) : y

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 2

Displaying data for employee
Employee number : 1
Employee name : Arthur Dent
Employee salary : 45000

Displaying data for employee
Employee number : 2
Employee name : Tricia McMillian
Employee salary : 12000

Displaying data for employee
Employee number : 3
Employee name : Ford Prefect
Employee salary : 50000

Displaying data for
employee

Employee number : 4
Employee name : Zaphod Beeblebrox
Employee salary : 30000

Do you want the menu to be shown again? (y / n) : y

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 4

Enter record number to delete : 2

Record you requested to be removed has been deleted

Do you want the menu to be shown again? (y / n) : y

Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 2

Displaying data for employee
Employee number : 1
Employee name : Arthur Dent
Employee salary : 45000

Displaying data for employee
Employee number : 3
Employee name : Ford Prefect
Employee salary : 50000

Displaying data for employee
Employee number : 4
Employee name : Zaphod Beeblebrox
Employee salary : 30000

Do you want the menu to be shown again? (y / n) : n

/ Program faults: It does not check whether record number to be deleted exceeds available number of records or not. Can be easily checked by taking value from countrec() and putting a check in delrec() */

Categories
Uncategorised

Binary Files – Floppy Box

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /

/ Binary Files – Floppy Box Management Program /

#include <fstream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class floppybox
{
int floppyid, size;
char name[42];
public:
void getdata()
{
cout<<endl<<“Enter floppy ID : “;
cin>>floppyid;
cout<<“Enter floppy name : “;
gets(name);
cout<<“Enter floppy size : “;
cin>>size;
}
void disp()
{
cout<<“\n\nDisplaying data for floppy”
<<endl<<“Floppy ID : “<<floppyid
<<endl<<“Floppy name : “<<name
<<endl<<“Floppy size : “<<size<<endl;
}
char
retname()
{
return name;
}
int retid()
{
return floppyid;
}
} obj1;

char ans;
fstream file;
int ch, flag;

void create()
{
file.open(“data”, ios::out | ios::binary);
do
{
obj1.getdata();
file.write((char)&obj1, sizeof(obj1));
cout<<“Do you wish to enter another record? (y / n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’);
file.close();
}

void showfile()
{
file.open(“data”, ios::in);
while (file.read((char
)&obj1, sizeof(obj1)))
obj1.disp();
file.close();
}

void countrec()
{
int r = 0;
file.open(“data”, ios::in);
while (file.read((char)&obj1, sizeof(obj1)))
++r;
file.close();
cout<<endl<<“Number
of records is “<<r<<endl;
}

void search()
{
file.open(“data”,
ios::in);
cout<<endl<<“Search according to”
<<endl<<“\t1. ID”
<<endl<<“\t2. Name”
<<endl<<“Enter your choice : “;
cin>>ch;
switch(ch)
{
case 1 : int id;
flag = 0;
cout<<endl<<“Enter
ID to search for : “;
cin>>id;
while (file.read((char
)&obj1,
sizeof(obj1)))
{
if (obj1.retid() == id)
{
obj1.disp();
flag = 1;
}
}
file.close();

if (flag == 0)

cout<<“\nID you entered does not exist\n”;
break;
case 2 : char nm[42];
flag = 0;
cout<<endl<<“Enter name to search for : “;
gets(nm);
while (file.read((char)&obj1, sizeof(obj1)))
{
if (strcmp(obj1.retname(),nm) == 0)
{
obj1.disp();
flag = 1;
}
}
file.close();
if (flag == 0)
cout<<“\nName you entered does not exist\n”;
break;
default : cout<<“\nYou entered an incorrect choice\n”;
}
}

void main()
{
clrscr();
do
{
cout<<endl<<“Floppy Box Management Program”
<<endl<<“1. Create file”
<<endl<<“2. Read file”
<<endl<<“3. Find total number of records”
<<endl<<“4. Search and display floppy details”
<<endl<<“Enter your choice : “;
cin>>ch;
switch(ch)
{
case 1 : create();
break;
case 2 : showfile();
break;
case 3 : countrec();
break;
case 4 : search();
break;
default : cout<<“\nYou entered an incorrect choice”;
}
cout<<“\nDo you want the menu to be shown again? (y/n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’ );
getch();
}

/ Output */

Floppy Box Management Program
1. Create file
2. Read file
3. Find total number of records
4. Search and display floppy details
Enter your choice : 1

Enter floppy ID : 1
Enter floppy name : Samsung
Enter floppy size : 4
Do you wish to enter another record? (y / n): y

Enter floppy ID : 2
Enter floppy name : Sony
Enter floppy size : 3
Do you wish to enter another record? (y / n): y

Enter floppy ID : 3
Enter floppy name : IBM
Enter floppy size : 2
Do you wish to enter another record? (y / n): n

Do you want the menu to be shown again? (y / n) : y

Floppy Box Management Program
1. Create file
2. Read file
3. Find total number of records
4. Search and display floppy details
Enter your choice : 2

Displaying data for floppy
Floppy ID : 1
Floppy name : Samsung
Floppy size : 4

Displaying data for floppy
Floppy ID : 2
Floppy name : Sony
Floppy size : 3

Displaying data for floppy
Floppy ID : 3
Floppy name : IBM
Floppy size : 2

Do you want the menu to be shown again? (y / n) : y

Floppy Box Management Program
1. Create file
2. Read file
3. Find total number of records
4. Search and display floppy details
Enter your choice : 3
Number of records is 3

Do you want the menu to be shown again? (y / n) : y

Floppy Box Management Program
1. Create file
2. Read file
3. Find total number of records
4. Search and display floppy details
Enter your choice : 4

Search according to
1. ID
2. Name
Enter your choice : 1

Enter ID to search for : 1

Displaying data for floppy
Floppy ID : 1
Floppy name : Samsung
Floppy size : 4

Do you want the menu to be shown again? (y / n) : y

Floppy Box Management Program
1. Create file
2. Read file
3. Find total number of records
4. Search and display floppy details
Enter your choice : 4

Search according to
1. ID
2. Name
Enter your choice : 2
Enter name to search for : IBM

Displaying data for floppy
Floppy ID : 3
Floppy name : IBM
Floppy size : 2

Do you want the menu to be shown again? (y / n) : n