0% found this document useful (0 votes)
111 views12 pages

C Day-15

From these documents you will able to learn step by step, and you will be able to create projects as well as you can be good programmer or a good teacher or trainer on your filed

Uploaded by

eshamu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views12 pages

C Day-15

From these documents you will able to learn step by step, and you will be able to create projects as well as you can be good programmer or a good teacher or trainer on your filed

Uploaded by

eshamu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

[ Binary ]

Session Objectives
Explain Binary file
Random access File modes Modifying,Deleting,viewing record in a file

#include<iostream.h> #include<fstream.h> #include<conio.h> #include<stdio.h> class employee { private: int eno; char ename[20]; float basic,pf,hra,net; public: void new_employee(); void del_employee(); void show_employee(); void modify_employee(); }; void employee::new_employee() { fstream f char wish; int lastno;

do {

f.open("emp.dat",ios::in|ios::binary); f.seekg(0,ios::end); if(f.tellg()==0) lastno=1; else { f.seekg(f.tellg()-sizeof(employee),ios::beg); f.read((char *)this,sizeof(employee)); lastno=eno+1; } f.seekg(0,ios::end); eno=lastno; cout<<"Employee number:"<<eno<<endl; cout<<"Enter the name:"; cin>>ename; cout<<"Enter basic:"; cin>>basic; cout<<"Enter pf:"; cin>>pf;

cout<<"Enter hra:"; cin>>hra; net=basic+pf+hra; f.write((char *)this,sizeof(employee)); f.close(); cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); } void employee::del_employee() { fstream f1,f2; char wish; int no,found; do { found=0; f1.open("emp.dat",ios::in); f2.open("temp.dat",ios::out); cout<<"Enter the employee number to be deleted:"; cin>>no; f1.read((char *)this,sizeof(employee));

} while(wish=='y'); } void employee::show_employee() { fstream f; int no,found; char wish;

while(!f1.eof()) { if(eno==no) found=1; else f2.write((char *)this,sizeof(employee)); f1.read((char *)this,sizeof(employee)); } if(!found) cout<<"record not found"; f1.close(); f2.close(); remove ("emp.dat"); rename("temp.dat","emp.dat"); cout<<"wish to record another employee :[y/n]:"; cin>>wish;

do { found=0; cout<<"enter the number to view:"; cin>>no; f.open("emp.dat",ios::in); f.read((char *)this,sizeof(employee)); while(!f.eof()) { if(eno==no) { found=1; cout<<"no"<<eno<<endl<<"ename"<<ename<<endl<<"basic:"<< basic<<endl<<"pf"<<pf<<endl<<"hra"<<hra<<endl<<"net"<< net<<endl; } f.read((char *)this,sizeof(employee)); } if(!found) cout<<"Employee record not found"<<endl; cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); }

void employee::modify_employee() { fstream f; int no,found; char wish; do { found=0; f.open("emp.dat",ios::in); cout<<"enter the employee number to modify:"; cin>>no; f.read((char *)this,sizeof(employee)); while(!f.eof()) { if(eno==no) { found=1; cout<<"ename:"<<ename<<endl; cout<<"Enter new basic:"; cin>>basic; cout<<"Enter new pf:"; cin>>pf; cout<<"Enter new hra:"; cin>>hra;

net=basic+pf+hra; f.seekp(f.tellg()-sizeof(employee)),ios::beg; f.write((char *)this,sizeof(employee)); } f.read((char *)this,sizeof(employee)); } if(!found) cout<<"Employee record not found"<<endl; cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); }

void main() { employee e; int choice; do { clrscr(); cout<<"1.Add a new employee record"<<endl; cout<<"2.Delete an existing record"<<endl; cout<<"3.Modify an existing record"<<endl;

cout<<"4.View an existing record"<<endl; cout<<"5.Exit"<<endl; cin>>choice; switch(choice) { case 1: e.new_employee(); break; case 2: e.del_employee(); break; case 3: e.modify_employee(); break; case 4: e.show_employee(); break; default: cout<<"Invalid Choice"; }} while(choice!=5); }

To write and read the contents in afile we must create an object of class ofstream & ifstream The open() function is used to open different files that uses the same stream object The close() function is used to close a file. It takes no parameters and returns no value The put() function is used for writing one character at a time in a file The get() function is used for reading one charater at a time from a file The write() function is member function of the ofstream class which writes text in a line until it encounters a new line character

EXERCISES
1. Write a Program to create Binary File using student

details such as Enrollment Number,Name,Course name , Duration and fees etc.,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy