RSS

Category Archives: Binary Files

SIMPLE PROGRAM TO CREATE A BINARY FILE

// THIS PROGRAM CREATES A BINARY FILE HAVING EMPLOYEE RECORDS

#include <fstream.h>
#include <conio.h>
#include “class.h” //MY OWN HEADER FILE
#include <stdio.h>
#include <process.h>

void main()
{
clrscr();

employee emp;
char wish = ‘Y’, add[20];
int i =0;
ofstream file;

cout<<“\nSUPPLY A NAME TO YOUR FILE\n”;
gets(add);

file.open(add,ios::binary);

if(!file)
{
cout<<“\nFILE COULD NOT BE OPENED\n”;
exit(1);
}

do
{
clrscr();
cout<<“\nENTER DATA FOR EMPLOYEE “<<++i;
emp.read();
file.write((char *)&emp,sizeof(emp));
cout<<“\nCONTINUE READING (Y/N)? \t”;
cin>>wish;
}
while(wish == ‘Y’);

cout<<“\nRECORDS WRITTEN SUCCESSFULLY TO DISK\n”;
cout<<“\nPRESS ANY KEY TO EXIT\n”;
getch();
}

Advertisements
 
Leave a comment

Posted by on August 17, 2015 in Binary Files

 

PROGRAM TO ADD RECORDS TO A BINARY FILE

// THIS PROGRAM ADDS RECORDS TO A BINARY FILE

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

class employee
{
char name[20];
int empid;
long salary;

public:
void read();
void print();
};

void main()
{
clrscr();
fstream file;

char add[2];

cout<<“\nENTER A NAME FOR YOUR FILE\n”;
gets(add);

file.open(add,ios::out|ios::binary);

employee emp;

char wish = ‘Y’;

do
{
emp.read();
file.write((char *)&emp,sizeof(emp));
cout<<“\nCONTINUE? (Y/N) “;
cin>>wish;
clrscr();
}
while(wish == ‘Y’);

cout<<“\nDATA SUCCESSFULLY WRITTEN TO DISK\n”;
file.close();

getch();
clrscr();
file.open(add,ios::app|ios::out|ios::in);

cout<<“\nENTER THE DETAILS OF THE EMPLOYEE WHOSE RECORD IS TO BE ADDED\n”;
emp.read();

file.write((char *)&emp,sizeof(emp));

cout<<“\nDATA SUCCESSFULLY WRITTEN\n”;
getch();
clrscr();
cout<<“\nDISPLAYING THE DATA \n\n\n”;
file.seekg(0);

int i = 0;

while(file.read((char *)&emp,sizeof(emp)))
{
cout<<“RECORD NUMBER “<<++i<<“\n\n\n”;
emp.print();
cout<<“\n\nPress any key to view next record\n\n”;
getch();
clrscr();
}

getch();
}

void employee :: read()
{
cout<<“\nENTER EMPLOYEE ID:\t”;
cin>>empid;
cout<<“\nENTER NAME:\t”;
gets(name);
cout<<“\nENTER SALARY\n”;
cin>>salary;
}

void employee :: print()
{
cout<<“\nEMPLOYEE ID :\t”<<empid;
cout<<“\n\nEMPLOYEE NAME:\t”;
puts(name);
cout<<“\nEMPLOYEE SALARY:\t”<<salary;
}

 
Leave a comment

Posted by on August 17, 2015 in Binary Files

 

PROGRAM TO UPDATE RECORDS IN A BINARY FILE

// THIS PROGRAM UPDATES RECORDS IN A BINARY FILE

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

struct product
{
int code;
char description[20];
int stock;
};

char add[20];
void update(fstream &);

void main()
{
clrscr();
fstream file;
cout<<“\nEnter the name of the file you want to open\n”;
gets(add);
file.open(add,ios::in|ios::out|ios::binary);

if(!file)
{
cout<<“\nTHE FILE COULD NOT BE OPNED\n”;
exit(-1);
}

char wish = ‘Y’;
product p;

do
{
clrscr();
cout<<“\nENTER THE PRODUCT CODE\n”;
cin>>p.code;
cout<<“\nENTER THE DESCRIPTION OF THE PRODUCT\n”;
gets(p.description);
cout<<“\nENTER THE STOCK\n”;
cin>>p.stock;
cout<<“\n\nALL DATA READ! \nDO YOU WANT TO READ MORE? (Y/N)\t”;
cin>>wish;
file.write((char *)&p,sizeof(p));
}
while(wish == ‘Y’);

cout<<“\nUPDATING THE RECORDS\n”;
file.seekg(0);
update(file);
cout<<“\nRECORDS UPDATED\n”;
cout<<“\nDISPLAYING THE RECORDS\n\n”;

while(file.read((char *)&p,sizeof(p)))
{
if(file.good() == 0)
break;
getch();
clrscr();
cout<<“\nCODE OF PRODUCT \n”;
cout<<p.code<<‘\n’;
cout<<“\nDESCRIPTION OF PRODUCT \n”;
puts(p.description);
cout<<“\nSTOCK AVAILABLE\n”<<p.stock;
cout<<“\n\nPress any key to continue\n”;
}
cout<<“\nALL RECORDS DISPLAYED\n”;
cout<<“\nPRESS ANY KEY TO EXIT\n”;
getch();
file.close();
}

void update(fstream &file)
{
product p;
cout<<“\nENTER THE OLD PRODUCT CODE OF THE RECORD YOU WANT TO UPDATE\n”;
int c;
cin>>c;
fstream temp(“temporary.dat”,ios::in|ios::out|ios::binary);

while(file.read((char *)&p,sizeof(p)))
{
if(file.good() == 0)
break;
if(p.code == c)
{
clrscr();
cout<<“\nMATCH FOUND\n”;
getch();
clrscr();
cout<<“\nENTER THE NEW PRODUCT CODE\n”;
cin>>p.code;
cout<<“\nENTER THE NEW DESCRIPTION\n”;
gets(p.description);
cout<<“\nENTER THE NEW STOCK QUANTITY\n”;
cin>>p.stock;
}
temp.write((char *)&p,sizeof(p));
}
file.close();
temp.close();
remove(add);
rename(“temporary.dat”,add);
file.open(add,ios::in|ios::out|ios::binary);
}

 
Leave a comment

Posted by on August 17, 2015 in Binary Files

 

PROGRAM TO DELETE RECORDS FROM A BINARY FILE

// THIS PROGRAM DELETES RECORDS FROM A BINARY FILE

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

class worker
{
private:
int id;
char name[30];
long salary;
public:
void read();
void print();
int getid()
{ return id; }
};

void worker::read()
{
cout<<“\nEnter ID:\t”;
cin>>id;
cout<<“\nEnter name:\t”;
gets(name);
cout<<“\nEnter salary:\t”;
cin>>salary;
}

void worker::print()
{
cout<<“\n\nID:\t”<<id;
cout<<“\nNAME:\t”;
puts(name);
cout<<“SALARY:\t”<<salary;
}

void main()
{
clrscr();
fstream file;
char add[20];
cout<<“\nEnter the name of the file you want to open\n”;
gets(add);
file.open(add,ios::in|ios::out|ios::binary);

if(!file)
{
cout<<“\nFILE COULD NOT BE OPENED\n”;
exit(-1);
}

cout<<“\nREADING THE RECORDS\n”;
char wish = ‘Y’;

worker w;

do
{
w.read();
cout<<“\nREAD MORE RECORDS (Y/N) ?\t”;
cin>>wish;
clrscr();
file.write((char *)&w,sizeof(w));
}
while(wish == ‘Y’ || wish == ‘y’);

cout<<“\nRECORDS WRITTEN TO DISK\n”;

cout<<“\nUPDATING THE RECORDS\n”;
int id;
cout<<“\nENTER THE ID OF THE WORKER WHOSE RECORD YOU WANT TO DELETE\n”;
cin>>id;
file.seekg(0);

fstream temp(“temporary.dat”,ios::in|ios::out|ios::binary);
int flag = 0;

while(file.read((char *)&w,sizeof(w)))
{
if(file.good() == 0)
break;
if(w.getid() == id)
{
cout<<“\nMATCH FOUND\n”;
getch();
clrscr();
file.read((char *)&w,sizeof(w));
flag = 1;
}
temp.write((char *)&w,sizeof(w));
}
if(flag == 1)
cout<<“\nRECORD DELETED\n”;
else
cout<<“\nNO MATCH FOUND !!!!\n”;
file.close();
temp.close();
remove(add);
rename(“temporary.dat”,add);
cout<<“\nDISPLAYING THE RECORDS\n”;
file.open(add,ios::in|ios::binary|ios::out);
file.clear();
file.seekg(0);

while(file.read((char *)&w,sizeof(w)))
{
if(file.good() == 0)
break;
w.print();
getch();
clrscr();
}
cout<<“\nTHANKS!! PRESS ANY KEY TO EXIT\n”;
getch();
}

 
Leave a comment

Posted by on August 17, 2015 in Binary Files

 

CHOICE BASED PROGRAM TO PERFORM OPERATIONS ON A BINARY FILE

// THIS PROGRAM PERFORMS VARIOUS OPERATIONS ON STUDENTS’ RECORDS

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

void addrec(fstream &);
void modrec(fstream &);
void delrec(fstream &);
void disptop(fstream &);
void display(fstream &);

class student
{
char name[30];
int cls;
int rno;
float marks;

public:
void read()
{
cout<<“\nENTER THE NAME \n”;
gets(name);
cout<<“\nENTER THE CLASS \n”;
cin>>cls;
cout<<“\nENTER THE ROLL NUMBER \n”;
cin>>rno;
cout<<“\nENTER MARKS\n”;
cin>>marks;
}
int getrno()
{ return rno; }

void print()
{
cout<<“\nR.NO:\t”<<rno;
cout<<“\n\nNAME:\t”;
puts(name);
cout<<“\nCLASS:\t”<<cls;
cout<<“\n\nMARKS:\t”<<marks;
}
float getmarks()
{ return marks; }
};

char add[20];

void main()
{
clrscr();
student stu;
char wish = ‘Y’;

cout<<“\nPLEASE SUPPLY A NAME FOR YOUR FILE\n”;
gets(add);

fstream file(add,ios::in|ios::out|ios::binary);

do
{
clrscr();
stu.read();
file.write((char *)&stu,sizeof(stu));
cout<<“\n\nCONTINUE ? (Y/N) “;
cin>>wish;
}
while(wish == ‘Y’);

wish = ‘Y’;

cout<<“\nTHANKS!!”;
do{
file.seekg(0);
clrscr();
cout<<“\nCHOOSE AN OPERATION\n”;
cout<<“\n1. ADD A RECORD\n”;
cout<<“\n2. MODIFY A RECORD\n”;
cout<<“\n3. DELETE A RECORD\n”;
cout<<“\n4. DISPLAY THE DETAILS OF THE TOPPER\n”;
cout<<“\n5. DISPLAY THE DETAILS OF ALL STUDENT\n”;
cout<<“\n6. EXIT FROM THE PROGRAM\n”;
int choice;
cin>>choice;
clrscr();
switch(choice)
{
case 1: addrec(file);
break;
case 2: modrec(file);
break;
case 3: delrec(file);
break;
case 4: disptop(file);
break;
case 5: display(file);
break;
case 6: exit(0);
}

cout<<“\nDO ANYTHING ELSE (Y/N) ?\t”;
cin>>wish;
}while(wish == ‘Y’ || wish == ‘y’);

cout<<“\n\nTHANKS!!!”;
getch();
}

void addrec(fstream &file)
{
file.seekg(0,ios::end);
student newstu;
cout<<“\nENTER THE DETAILS OF THE NEW STUDENT \n”;
newstu.read();
file.write((char *)&newstu,sizeof(newstu));
cout<<“\n\nDATA SUCESSFULLY ADDED\n”;
cout<<“\n\nPRESS ANY KEY TO CONTINUE\n”;
getch();
clrscr();
file.seekg(0);
}

void modrec(fstream &file)
{
cout<<“\nENTER THE ROLL NUMBER OF THE STUDENT WHOSE RECORD YOU WANT TO MODIFY\n\n”;
int r;
cin>>r;
int flag = 0;

student s;
fstream temp(“temp.dat”,ios::in|ios::out|ios::binary);

while(file.read((char *)&s,sizeof(s)))
{
if(s.getrno() == r)
{
flag++;
cout<<“\nENTER THE MODIFIED DETAILS OF THE STUDENT\n”;
s.read();
}
temp.write((char *)&s,sizeof(s));
}

if(flag == 0)
cout<<“\nRECORD NOT FOUND!!!!\n”;
else
cout<<“\nRECORD MODIFIED\n”;
cout<<“\nPRESS ANY KEY TO CONTINUE\n”;
getch();
file.close();
temp.close();
remove(add);
rename(“temp.dat”,add);
file.open(add,ios::in|ios::out|ios::binary);
file.seekg(0);
}

void delrec(fstream &file)
{
int flag = 0;
int roll;

cout<<“\nENTER THE ROLL NUMBER OF THE STUDENT WHOSE DATA YOU WANT TO DELETE\n”;
cin>>roll;

fstream temp(“temp.dat”,ios::in|ios::out|ios::binary);
student s;

while(file.read((char *)&s,sizeof(s)))
{
if(s.getrno() == roll)
{
file.read((char *)&s,sizeof(s));
flag = 1;
}
temp.write((char *)&s,sizeof(s));
}

if(flag == 1)
{
cout<<“\nRECORD DELETED\n”;
}
else
{
cout<<“\nNO SUCH STUDENT EXISTS\n”;
}

file.close();
temp.close();
remove(add);
rename(“temp.dat”,add);
file.open(add,ios::in|ios::out|ios::binary);
file.seekg(0);
file.clear();
}

void disptop(fstream &file)
{
float val = 0;

student s,topper;

file.read((char *)&s,sizeof(s));

val = s.getmarks();
topper = s;
file.clear();

while(file.read((char *)&s,sizeof(s)))
{
if(s.getmarks() > val)
{
val = s.getmarks();
topper = s;
}
}

cout<<“\nTHE DETAILS OF TOPPER ARE\n\n\n”;
topper.print();
file.seekg(0);
file.clear();
}

void display(fstream &file)
{
student s;
int i = 1;
file.seekg(0);
while(file.read((char *)&s,sizeof(s)))
{
cout<<“\nSTUDENT “<<i++<<” DETAILS\n\n”;
s.print();
cout<<“\n\nPRESS ANY KEY TO CONTINUE….\n\n”;
getch();
clrscr();
}
cout<<“\n\nALL RECORDS DISPLAYED\n”;
file.seekg(0);
file.clear();
}

 
Leave a comment

Posted by on August 17, 2015 in Binary Files