Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Program 7 Q: Write a program to create a binary file employee.dat with n records,each record having eno, ename and salary.

Display the details of those employees whose salary is between 10,000 and 20,000. Note: Creation to be done in a function called create and display in a function called display. Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <stdio.h> 4. <fstream.h> Data types used: 1. int 2. char 3. float 4. ifstream 5. ofstream 6. emp e Program: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> struct emp { int eno; char ename[25]; float salary; }; void create() { emp E; ofstream f1; f1.open("EMPLOYEE.DAT",ios::binary); int n; cout<<" Enter no. employees - "; cin>>n; for(int i=1;i<=n;i++) { cout<<endl;

cout<<" Enter eno - "; cin>>E.eno; cout<<" Enter name - "; gets(E.ename); cout<<" Enter salary "; cin>>E.salary; f1.write((char*)&E,sizeof(E)); } f1.close(); } void display() { emp E; ifstream f1; f1.open("EMPLOYEE.DAT",ios::binary); while(f1.read((char*)&E,sizeof(E))) { if(E.salary>=10000&&E.salary<=20000) { cout<<endl; cout<<" "<<E.eno<<endl; cout<<" ";puts(E.ename); cout<<" "<<E.salary<<endl; } } f1.close(); } void main() { clrscr(); create(); display(); getch(); }

Output:

Program 8

Q: Write a program to create a binary file student.dat with grno, name and gender (F/M) with n records in it. Create two files girl.dat and boy.dat which will have details from student.dat accordingly in a function called split(). Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <stdio.h> 4. <fstream.h> Data Types Used: 1. stud 2. ofstream 3. ifstream 4. int 5. char Program: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> struct stud { int gno; char na[50]; char g; }; void create() { stud s; ofstream f1; f1.open("STUDENT.DAT",ios::binary); int n ; cout<<" Enter no. of students - "; cin>>n; for(int i=1;i<=n;i++) { cout<<endl; cout<<" Enter GR no - "; cin>>s.gno; cout<<" Enter name -"; gets(s.na);

cout<<" Enter Gender - "; cin>>s.g; f1.write((char*)&s,sizeof(s)); } f1.close(); } void split() { stud s; ifstream f1; ofstream f2, f3; f1.open("STUDENT.DAT", ios::binary); f2.open("GIRL.DAT", ios::binary); f3.open("BOY.DAT",ios::binary); while(f1.read((char*)&s, sizeof(s))) { if(s.g=='F') f2.write((char*)&s, sizeof(s)); else f3.write((char*)&s, sizeof(s)); } f1.close(); f2.close(); f3.close(); } void display() { stud s; ifstream f3,f2; f2.open("GIRL.DAT", ios::binary); f3.open("BOY.DAT",ios::binary); cout<<endl<<" Girls "<<endl; while( f2.read((char*)&s, sizeof(s))) { cout<<endl; cout<<" Name - "<< s.na<<endl; cout<<" GR NO. -"<<s.gno<<endl; } cout<<endl<<" Boys "<<endl; while( f3.read((char*)&s, sizeof(s))) { cout<<endl; cout<<" Name - "<< s.na<<endl; cout<<" GR NO. -"<<s.gno; } f2.close(); f3.close();

} void main() { clrscr(); create(); split(); display(); getch(); } Output:

Program 9 Q: Write a program to create a data file book.dat with bno and bname. Delete a record from the file taking bno as the key field.

Header Files Used: 1. <iostream.h> 2. <stdio.h> 3. <conio.h> 4. <fstream.h> Data Types Used: 1. book B 2. int 3. char 4. ofstream 5. ifstream Program: #include <iostream.h> #include <stdio.h> #include <conio.h> #include <fstream.h> struct book { int bno; char bna[25]; }; void main() { clrscr(); int n; book B; ofstream f1; f1.open("BOOK.DAT",ios::binary); cout<<" Enter n "; cin>>n; for(int i=1;i<=n;i++) { cout<<endl; cout<<" Enter bno "; cin>>B.bno; cout<<" Enter book name "; gets(B.bna); f1.write((char*)&B,sizeof(B)); } f1.close(); ifstream f2; f2.open("BOOK.DAT",ios::binary);

ofstream f3; f3.open("BOOK1.DAT",ios::binary); int x; cout<<endl<<" Enter book number to delete "; cin>>x; while(f2.read((char*)&B,sizeof(B))) { if(B.bno!=x) { f3.write((char*)&B,sizeof(B)); } } f3.close(); f2.close(); remove("BOOK.DAT"); rename("BOOK1.DAT","BOOK.DAT"); f2.open("BOOK.DAT",ios::binary); while(f2.read((char*)&B,sizeof(B))) { cout<<endl; cout<<" Book number = "<<B.bno<<endl; cout<<" Book name = "<<B.bna<<endl<<endl; } getch(); } Output:

Progra m 10 Q: Write a progra m to declare a class employee with data members eno and ename, member functions getdata() and putdata(). Write a menu driven program in C++ to:

(i) Create a binary file with n objects by entering eno in ascending order (ii) To insert a new object in the correct location (iii) To display Header Files Used: 1. <iostream.h> 2. <stdio.h> 3. <conio.h> 4. <fstream.h> 5. <process.h> Data Types Used: employee 1. ofstream 2. ifstream 3. int 4. char Program: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> #include <process.h> class employee { int eno; char ename[25]; public: void getdata() { cout<<endl; cout<<" Enter employee number "; cin>>eno; cout<<" Enter employee name "; gets(ename); } void putdata() { cout<<" Employee number - "<<eno<<" "<<" Employee name - "<<ename<<endl; } int ret() { return(eno);

} }; void create(); void ins(); void display(); void main() { int x; l: clrscr(); cout<<" 1. Create "<<endl; cout<<" 2. Insert "<<endl; cout<<" 3. Display "<<endl; cout<<" 4. Exit "<<endl; cout<<" Enter choice "; cin>>x; switch(x) { case 1:create(); break; case 2:ins(); break; case 3:display(); break; case 4:exit(0); } getch(); goto l; } void create() { employee E; ofstream f1; int n; cout<<endl<<" Enter n "; cin>>n; f1.open("EMP.DAT",ios::binary); for(int i=1;i<=n;i++) { E.getdata(); f1.write((char*)&E,sizeof(E)); } f1.close(); } void display()

{ ifstream f1; f1.open("EMP.DAT",ios::binary); employee E; while(f1.read((char*)&E,sizeof(E))) { E.putdata(); } f1.close(); } void ins() { char ch='n'; ifstream f2; ofstream f1; employee E,X; X.getdata(); f2.open("EMP.DAT",ios::binary); f1.open("NEMP.DAT",ios::binary); while(f2.read((char*)&E,sizeof(E))) { if(E.ret()>X.ret()&&ch=='n') { f1.write((char*)&X,sizeof(X)); f1.write((char*)&E,sizeof(E)); ch='y'; } else f1.write((char*)&E,sizeof(E)); } if(ch=='n') f1.write((char*)&X,sizeof(X)); f1.close(); f2.close(); remove("EMP.DAT"); rename("N EMP.DAT", "EMP.DAT" ); } Output

Program 11 Q: Write a program to create a data file and search for a record where the file is opened in fstream mode. Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <fstream.h> 4. <stdio.h> Data Types Used: 1. stud 2. int 3. char

4. fstream Program: #include <iostream.h> #include <conio.h> #include <fstream.h> #include <stdio.h> struct stud { int grno; char na[18]; }; void main() { clrscr(); int n,x; char ch='n'; fstream f1; f1.open("STUD.DAT",ios::binary|ios::in|ios::out); stud S; cout<<" Enter n "; cin>>n; for(int i=1;i<=n;i++) { cout<<endl; cout<<" Enter GR No "; cin>>S.grno; cout<<" Enter name "; gets(S.na); f1.write((char*)&S,sizeof(S)); } cout<<endl<<" Enter grno of record to be searched "; cin>>x; f1.seekg(0,ios::beg); for(i=1;i<=n;i++) { f1.read((char*)&S,sizeof(S)); if(x==S.grno) { cout<<" "<<S.grno<<" "; puts(S.na); ch='y'; break; } }

if(ch=='n') cout<<" Not found "; getch(); }

Output:

Program 12 Q: Write a program to append more records into an existing file student.dat assuming class stud has data members grno, name and two functions getdata() and putdata(). Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <stdio.h> 4. <fstream.h> Data Types Used: 1. stud S 2. int 3. char 4. ofstream 5. fstream

Program: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> class stud { int grno; char na[25]; public: void getdata() { cout<<endl; cout<<" Enter grno "; cin>>grno; cout<<" Enter name "; gets(na); } void putdata() { cout<<endl; cout<<" Grno: "<<grno<<" "; cout<<" Name: "<<na<<endl; } }; void main() { clrscr(); int n; stud S; ofstream f; f.open("STUDENT.DAT",ios::binary); cout<<" Enter n "; cin>>n; for(int i=1;i<=n;i++) { S.getdata(); f.write((char*)&S,sizeof(S)); } f.close(); fstream f1; f1.open("STUDENT.DAT",ios::binary|ios::app|ios::in); cout<<endl<<" Enter number of records to add - "; cin>>n; for(i=1;i<=n;i++) {

S.getdata(); f1.write((char*)&S,sizeof(S)); } f1.seekg(0,ios::beg); while(f1.read((char*)&S,sizeof(S))) S.putdata(); f1.close(); getch(); } Output:

Program 13 Q: Write a program in C++ to create a data file and modify a record without using a 2nd file. Header Files Used:

1. 2. 3. 4.

<iostream.h> <conio.h> <stdio.h> <fstream.h>

Data Types Used: 1. stud S 2. int 3. char 4. fstream 5. ofstream 6. ifstream Program: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> struct stud { int rno; int grno; char na[25]; }; void modify() { stud S; int x; fstream f1; f1.open("STUDENT.DAT",ios::binary|ios::in|ios::out); cout<<" Enter x "; cin>>x; f1.seekp((x-1)*sizeof(S),ios::beg); cout<<" Enter Record number "; cin>>S.rno; cout<<" Enter Grno "; cin>>S.grno; cout<<" Enter name "; gets(S.na); f1.write((char*)&S,sizeof(S)); f1.seekg(0,ios::beg); while(f1.read((char*)&S,sizeof(S))) { cout<<" Record no= "<<S.rno<<" "<<" Grno= "<<S.grno<<" "<<" Name - "<<S.na<<endl; }

f1.close(); } void main() { clrscr(); stud S; int n; cout<<" Enter n "; cin>>n; ofstream f1; f1.open("STUDENT.DAT",ios::binary); for(int i=1;i<=n;i++) { cout<<endl; cout<<" Enter Record number "; cin>>S.rno; cout<<" Enter GR no "; cin>>S.grno; cout<<" Enter name "; gets(S.na); f1.write((char*)&S,sizeof(S)); } f1.close(); ifstream f2; f2.open("STUDENT.DAT",ios::binary); while(f2.read((char*)&S,sizeof(S))) cout<<" Record no = "<<S.rno<<" "<<" Grno = "<<S.grno<<" "<<" Name is - "<<S.na<<endl; f2.close(); modify(); getch(); }

Outpu t:

Program 14 Q: Write a program in C++ to create a binary file student.dat and count the number of records in it. Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <stdio.h> 4. <fstream.h> Data Types Used: 1. stud 2. int 3. char 4. ofstream 5. fstream Program: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <fstream.h> class stud

{ int grno; char na[25]; public: void getdata() { cout<<endl; cout<<" Enter grno "; cin>>grno; cout<<" Enter name "; gets(na); } }; void main() { clrscr(); stud S; ofstream f1; f1.open("STUDENT.DAT",ios::binary); int n; cout<<" Enter n "; cin>>n; for(int i=1;i<=n;i++) { S.getdata(); f1.write((char*)&S,sizeof(S)); } f1.close(); fstream f; f.open("STUDENT.DAT",ios::binary|ios::in); f.seekg(0,ios::end); int x=f.tellg(); int count=x/sizeof(S); cout<<" Number of records is - "<<count; f.close(); getch(); } Output:

Program 15 Q: Write a program to create a text file with n lines of text. Count and print the number of lines starting with alphabet A. Header Files Used: 1. <iostream.h> 2. <stdio.h> 3. <conio.h> 4. <fstream.h> Data Types Used: 1. int 2. char 3. ofstream 4. ifstream Program: #include <iostream.h> #include <stdio.h> #include <conio.h> #include <fstream.h> void main()

{ clrscr(); ofstream f1; char a[80]; int c=0; f1.open("TEXT.TXT"); int n; cout<<" Enter n "; cin>>n; cout<<endl; for(int i=1;i<=n;i++) { cout<<" Enter a line of text "; gets(a); for(int j=0;a[j]!='\0';j++) f1.put(a[j]); f1.put('\n'); } f1.close(); ifstream f2; f2.open("TEXT.TXT"); cout<<endl; while(f2) { f2.getline(a,80); if(a[0]=='a'||a[0]=='A') { puts(a); c++; } } cout<<" Number of lines starting with A - "<<c; f2.close(); getch(); } Output:

Program 16 Q: Write a program to create a text file and count the number of times the word the appears in the full file. Header Files Used: 1. <iostream.h> 2. <conio.h> 3. <fstream.h> 4. <stdio.h> 5. <string.h> Data Types Used: 1. int 2. char 3. ofstream 4. ifstream Program:

#include <iostream.h> #include <conio.h> #include <fstream.h> #include <stdio.h> #include <string.h> void main() { clrscr(); ofstream f1; char a[80]; f1.open("TEXT1.TXT"); int n; cout<<" Enter n "; cin>>n; for(int i=1;i<=n;i++) { cout<<" Enter a line of text - "; gets(a); for(int j=0;a[j]!='\0';j++) f1.put(a[j]); f1.put('\n'); } f1.close(); ifstream f2; int c=0; f2.open("TEXT1.TXT"); while(f2) { f2.getline(a,80); if(a[0]=='T'&&a[1]=='h'||a[2]=='e'&&a[3]==' ') c++; for(int i=0;i<strlen(a);i++) { if(a[i]==' ') { if(a[i+1]=='t'||a[i+1]=='T') { if(a[i+2]=='h'||a[i+2]=='H') { if(a[i+3]=='e'||a[i+3]=='E') { if(a[i+4]==' '||a[i+4]=='\0') c++; } }

} } } } cout<<endl<<" Number of times the word 'the' appears - "<<c; f2.close(); getch(); } Output:

Program 17 Q: Write a program to create a text file in C++ with n lines of text. Create a new file where the first alphabet of every word is converted to capital letter. Header Files Used: 1. <iostream.h> 2. <stdio.h> 3. <conio.h> 4. <fstream.h> 5. <ctype.h> Data Types Used: 1. int 2. char 3. ofstream 4. ifstream Program: #include <iostream.h> #include <stdio.h> #include <conio.h>

#include <fstream.h> #include <ctype.h> void main() { clrscr(); ofstream f1; char a[80]; f1.open("TEXT2.TXT"); int n; cout<<" Enter n "; cin>>n; cout<<endl; for(int i=1;i<=n;i++) { cout<<" Enter a line of text "; gets(a); for(int j=0;a[j]!='\0';j++) f1.put(a[j]); f1.put('\n'); } f1.close(); ifstream f2; f2.open("TEXT2.TXT"); f1.open("NEW.TXT"); while(f2) { f2.getline(a,80); a[0]=toupper(a[0]); for(int i=0;a[i]!='\0';i++) { if(a[i]==' ') a[i+1]=toupper(a[i+1]); } for(i=0;a[i]!='\0';i++) f1.put(a[i]); f1.put('\n'); } f1.close(); f2.close(); f2.open("NEW.TXT"); cout<<endl; while(f2) { f2.getline(a,80); puts(a);

} getch(); } Output:

You might also like