Google PlusFacebookTwitter

Binary Files – Floppy Box

By on Sep 5, 2007 in On A Whim | 0 comments

Share On GoogleShare On FacebookShare On Twitter

/* 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



Submit a Comment

Your email address will not be published. Required fields are marked *