Sie sind auf Seite 1von 7

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION
Write a program to perform the following operation in a binary file "student.dat" with the help of the given class class student { int no,age; char name[30]; public: void input()- A function to input all the details of the student. void output()- A function to display the details of the student. long retno()- A function to return the student roll no. }; Functions outside the class(Non-member functions):A function to create a binary file with n no. of sorted records on to a file. A function to display the contents of the file. A function to insert a new record on the sorted file. Write the main program to perform the various operations with the help of the menu driven program.

DOCUMENTATION 1.Header file a.iostream.h cin - to input the values from the user. cout- to display values on the screen. b.conio.h clrsrc()- clear the screen. getch()- for holding the screen and input a character. c.stdio.h gets()- to input the characters from the user. d.iomanip.h setw()- to set a specific set of character and spacing. e.fstream.h read()- to read the records of a life.
Indian Community School, Kuwait 2012-2013 49 Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION write()- to write the records into a file. 2.List of variables used int no- to store the student's roll number. char name[30]- to store the name of the student. int age-to store the age of the student. int flag- to insert a student to a file. int ch- for the choice of the user. Brief note of the program This a program to input, display and insert the details of the students and store the information in a given file, student.dat, according to user's choice. Brief note of the functions input()- to input the name, roll number and age of the student. output()- to output the name, age and student number of the student. retno()- to return the roll number of the student. insertion()- to insert a particular record based on the student number. display()- to display all the records of the file, "student.dat". create()- to create a file "student.dat" and add records to the file.

Program Listing #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> #include<iomanip.h> class student {


Indian Community School, Kuwait 2012-2013 50 Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION int no; char name[30]; int age; public: void input(); void output(); int retno() { return no; } }; void student::input() { cout<<"\n\nEnter the roll number: "; cin>>no; cout<<"\n\nEnter the name: "; gets(name); cout<<"\n\nEnter the age: "; cin>>age; } void student::output() { cout<<"\n\t\t"<<setw(10)<<no<<setw(10)<<name<<setw(10)<<age; } void create() { int n; cout<<"\n\nEnter the number of records to be added "; student ob; cin>>n; ofstream fout("student.dat",ios::binary|ios::app);
Indian Community School, Kuwait 2012-2013 51 Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION for(int i=0;i<n;i++) { ob.input(); fout.write((char*)&ob,sizeof(ob)); } fout.close(); } void display(char* file) { student ob; ifstream fin(file,ios::binary); cout<<"\n\n\t\t"<<setw(10)<<"Roll no"<<setw(10)<<"Name"<<setw(10)<<"Age"; while(fin.read((char*)&ob,sizeof(ob))) ob.output(); fin.close(); } void insertion() { int flag=0; student ob; student ob1; ifstream fin("student.dat",ios::binary); ofstream fout("new.dat",ios::binary|ios::app); ob1.input(); while(fin.read((char*)&ob,sizeof(ob))) { if(ob1.retno()<ob.retno() && flag==0) { fout.write((char*)&ob1,sizeof(ob1)); flag=1; }
Indian Community School, Kuwait 2012-2013 52 Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION fout.write((char*)&ob,sizeof(ob)); } fin.close(); fout.close(); } void heading() { cout<<"\t\t\t8.FILE HANDLING- INSERTION\n"; cout<<"\t\t\t___________________________"; } void main() { int ch; do { clrscr(); heading(); cout<<"\n\n1.Add student"; cout<<"\n2.Display old file "; cout<<"\n3.Insert student"; cout<<"\n4.Display new file"; cout<<"\n5.Exit"; cout<<"\n\nEnter the choice: "; cin>>ch; switch(ch) { case 1: create(); break; case 2: display("student.dat"); break; case 3: insertion(); break; case 4: display("new.dat");
Indian Community School, Kuwait 2012-2013 53 Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION break; default: cout<<"\n\nInvalid choice"; } getch(); }while(ch<5); getch(); } Output 8.FILE HANDLING- INSERTION _______________________________ 1.Add students 2.Display the old file 3.Insert new student 4.Display the new file 1. Enter your choice 2 Roll.No 21 23 2.Enter your choice 3 Enter the Roll number:22 Enter the name: Ramesh Enter the age: 20 Emp.No 21
Indian Community School, Kuwait 2012-2013

Name Vivek Raj

Age 17 18

Name Vivek
54

Age 17
Vivek Unnikrishnan 12th -A

DATE:- 27 Oct,2012

rd

10.FILE HANDLING-INSERTION 22 23 Ramesh Raj 20 18

4.Enter your choice 4 Emp.No 21 22 23 Name Vivek Ramesh Raj Age 17 20 18

Indian Community School, Kuwait 2012-2013

55

Vivek Unnikrishnan 12th -A

Das könnte Ihnen auch gefallen