Reading/Writing Structure Variable
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
struct student
{
int rollno;
char name[20];
float marks;
};
void main()
{
fstream fout;
student s1;
char ch = 'y';
clrscr();
fout.open(“student.dat“ , ios::out);
while(ch!='n')
{
cout<<"Enter Roll No.";
cin>>s1.rollno;
cout<<"nEnter Name:";
gets(s1.name)
cout<<"nEnter marks:";
cin>>s1.marks;
fout.write((char *)&s1 , sizeof(s1));
cout<<"nDo you want to add more data:";
cin>>ch;
}
fout.close();
cout<<"nnData in file......";
fout.open("dataread.dat",ios::in);
fout.read((char *)&s1 , sizeof(s1)) ;
while(fout)
{
cout<<"nRollNo: "<<s1.rollno<<"t";
cout<<"Name: "<<s1.name<<"t";
cout<<"Marks: "<<s1.marks;
fout.read((char *)&s1 , sizeof(s1));
}
fout.close();
getch();
}
Reading/Writing class Object
class student
{ int rollno;
char name[20];
float marks;
public:
void getdata();
void showdata();
};
void student::getdata()
{ cout<<"Enter Roll No.";
cin>>rollno;
cout<<"nEnter Name:";
gets(name);
cout<<"nEnter marks:";
cin>>marks; }
void student::showdata()
{
cout<<rollno<<"t"<<name<<"t"<<marks<<"n";
}
void main()
{ fstream sfile;
student s1;
char ch = 'y';
sfile.open("dataread.dat",ios::out);
while(ch!='n')
{
s1.getdata();
sfile.write((char *)&s1 , sizeof(s1));
cout<<"nDo you want to add more data:";
cin>>ch;
}
sfile.close();
cout<<"nnData in file......n";
sfile.open("dataread.dat",ios::in);
sfile.read((char *)&s1 , sizeof(s1)) ;
while(sfile)
{
s1.showdata();
sfile.read((char *)&s1 , sizeof(s1));
}
sfile.close();
getch();
}
Q.1- Consider the following class declaration:
class employee
{
int code;
char name[20];
float salary;
public:
void input()
{
cin>>code>>name>>salary;
}
void show()
{
cout<<code<<name<<salary<<endl;
}
float retsal()
{
return salary;
}
};
(i) Write a function to write the objects of employee to a binary file.
(ii) Write a function to read objects of employee from the file and display all the objects
on the screen whose salary is between 10000 and 20000.
Ans.
void writeemp()
{
employee emp;
ofstream fout;
fout.open(“employee.dat”,ios::out);
int n,I;
cout<<“Enter no. of records you want to insert”;
cin>>n;
for(i=1 ; i<=n ; i++)
{
emp.input();
fout.write((char *)&emp , sizeof(emp));
}
fout.close();
}
void readdata()
{
employee emp;
ifstream fin;
float esal;
fin.open(“employee.dat” , ios::in);
if(!fin)
cout<<“file does not exist”;
fin.read((char *)&emp , sizeof(emp));
while(fin)
{
esal = emp.retsal();
if(esal>=10000 && esal<=20000)
{
emp.show();
}
fin.read((char *)&emp , sizeof(emp));
}
fin.close();
}
Q.2- Consider the following class declaration:
class camera
{
long modelno;
float megapixel;
char detail[20];
public:
void enter()
{
cin>>modelno>>megapixel>>detail;
}
void display()
{
cout<<modelno<<megapixel<<detail<<endl;
}
long retModel()
{
return modelno;
}
};
Write a function to search for a camera from binary file “camera.dat”. The user will
enter modelno and this function will search and display the complete detail of camera.
Ans.
void search()
{
camera cam;
ifstream fin;
long cmodel;
cout<<“Enter model of camera to be searched ”;
cin>>cmodel;
fin.open(“camera.dat” , ios::in);
if(!fin)
cout<<“file does not exist”;
fin.read((char *)&cam , sizeof(cam));
while(fin)
{
if(cam.retModel() = = cmodel)
{
cam.display();
}
fin.read((char *)&emp , sizeof(emp));
}
fin.close();
}

More Related Content

TXT
c++ program for Railway reservation
TXT
Railway reservation
PPSX
C++ programming structure & union
TXT
Railwaynew
DOCX
RAILWAY RESERWATION PROJECT PROGRAM
DOCX
PPTX
File Handling in C++ full ppt slide presentation.ppt
c++ program for Railway reservation
Railway reservation
C++ programming structure & union
Railwaynew
RAILWAY RESERWATION PROJECT PROGRAM
File Handling in C++ full ppt slide presentation.ppt

Similar to PythonProgramming-Binary-file-handling-2.pptx (20)

PPSX
C programming structure & pointer
PPTX
Singly linked list.pptx
DOCX
Start with the inclusion of libraries#include iostream .docx
PDF
C program
DOC
C basics
PDF
Object Oriented Programming (OOP) using C++ - Lecture 2
DOCX
Cmptr ass
PDF
C programms
PDF
Program to sort the n names in an alphabetical order
DOCX
Student Data Base Using C/C++ Final Project
PDF
Data structure week 3
PPTX
Decision making and branching
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PDF
polymorphism in c++ with Full Explanation.
DOCX
File handling in c++
DOCX
Data structures
DOCX
Student DATABASE MANAGeMEnT SysTEm
C programming structure & pointer
Singly linked list.pptx
Start with the inclusion of libraries#include iostream .docx
C program
C basics
Object Oriented Programming (OOP) using C++ - Lecture 2
Cmptr ass
C programms
Program to sort the n names in an alphabetical order
Student Data Base Using C/C++ Final Project
Data structure week 3
Decision making and branching
Object Oriented Programming (OOP) using C++ - Lecture 3
polymorphism in c++ with Full Explanation.
File handling in c++
Data structures
Student DATABASE MANAGeMEnT SysTEm
Ad

Recently uploaded (20)

DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
English Textual Question & Ans (12th Class).pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
My India Quiz Book_20210205121199924.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Journal of Dental Science - UDMY (2021).pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
English Textual Question & Ans (12th Class).pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
What’s under the hood: Parsing standardized learning content for AI
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
Empowerment Technology for Senior High School Guide
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
Core Concepts of Personalized Learning and Virtual Learning Environments
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Ad

PythonProgramming-Binary-file-handling-2.pptx

  • 1. Reading/Writing Structure Variable #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> struct student { int rollno; char name[20]; float marks; }; void main() { fstream fout; student s1; char ch = 'y'; clrscr(); fout.open(“student.dat“ , ios::out); while(ch!='n') { cout<<"Enter Roll No."; cin>>s1.rollno; cout<<"nEnter Name:"; gets(s1.name) cout<<"nEnter marks:"; cin>>s1.marks; fout.write((char *)&s1 , sizeof(s1)); cout<<"nDo you want to add more data:"; cin>>ch; } fout.close(); cout<<"nnData in file......"; fout.open("dataread.dat",ios::in); fout.read((char *)&s1 , sizeof(s1)) ; while(fout) { cout<<"nRollNo: "<<s1.rollno<<"t"; cout<<"Name: "<<s1.name<<"t"; cout<<"Marks: "<<s1.marks; fout.read((char *)&s1 , sizeof(s1)); } fout.close(); getch(); }
  • 2. Reading/Writing class Object class student { int rollno; char name[20]; float marks; public: void getdata(); void showdata(); }; void student::getdata() { cout<<"Enter Roll No."; cin>>rollno; cout<<"nEnter Name:"; gets(name); cout<<"nEnter marks:"; cin>>marks; } void student::showdata() { cout<<rollno<<"t"<<name<<"t"<<marks<<"n"; } void main() { fstream sfile; student s1; char ch = 'y'; sfile.open("dataread.dat",ios::out); while(ch!='n') { s1.getdata(); sfile.write((char *)&s1 , sizeof(s1)); cout<<"nDo you want to add more data:"; cin>>ch; } sfile.close(); cout<<"nnData in file......n"; sfile.open("dataread.dat",ios::in); sfile.read((char *)&s1 , sizeof(s1)) ; while(sfile) { s1.showdata(); sfile.read((char *)&s1 , sizeof(s1)); } sfile.close(); getch(); }
  • 3. Q.1- Consider the following class declaration: class employee { int code; char name[20]; float salary; public: void input() { cin>>code>>name>>salary; } void show() { cout<<code<<name<<salary<<endl; } float retsal() { return salary; } }; (i) Write a function to write the objects of employee to a binary file. (ii) Write a function to read objects of employee from the file and display all the objects on the screen whose salary is between 10000 and 20000.
  • 4. Ans. void writeemp() { employee emp; ofstream fout; fout.open(“employee.dat”,ios::out); int n,I; cout<<“Enter no. of records you want to insert”; cin>>n; for(i=1 ; i<=n ; i++) { emp.input(); fout.write((char *)&emp , sizeof(emp)); } fout.close(); } void readdata() { employee emp; ifstream fin; float esal; fin.open(“employee.dat” , ios::in); if(!fin) cout<<“file does not exist”; fin.read((char *)&emp , sizeof(emp)); while(fin) { esal = emp.retsal(); if(esal>=10000 && esal<=20000) { emp.show(); } fin.read((char *)&emp , sizeof(emp)); } fin.close(); }
  • 5. Q.2- Consider the following class declaration: class camera { long modelno; float megapixel; char detail[20]; public: void enter() { cin>>modelno>>megapixel>>detail; } void display() { cout<<modelno<<megapixel<<detail<<endl; } long retModel() { return modelno; } }; Write a function to search for a camera from binary file “camera.dat”. The user will enter modelno and this function will search and display the complete detail of camera.
  • 6. Ans. void search() { camera cam; ifstream fin; long cmodel; cout<<“Enter model of camera to be searched ”; cin>>cmodel; fin.open(“camera.dat” , ios::in); if(!fin) cout<<“file does not exist”; fin.read((char *)&cam , sizeof(cam)); while(fin) { if(cam.retModel() = = cmodel) { cam.display(); } fin.read((char *)&emp , sizeof(emp)); } fin.close(); }