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

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

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

.dat with bno and bname. display(). create(). split(). getch(). } Output: Program 9 Q: Write a program to create a data file book.} void main() { clrscr(). Delete a record from the file taking bno as the key field.

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

cout<<" Book number = "<<B. cout<<" Book name = "<<B.ios::binary). } Output: Progra m 10 Q: Write a progra m to declare a class employee with data members eno and ename.close(). remove("BOOK. f2. while(f2. cout<<endl<<" Enter book number to delete ".DAT"). member functions getdata() and putdata().DAT").open("BOOK1.read((char*)&B. } } f3. } getch(). Write a menu driven program in C++ to: .bna<<endl<<endl.bno!=x) { f3.DAT".sizeof(B))) { if(B.read((char*)&B."BOOK. f3. while(f2. rename("BOOK1. int x.write((char*)&B.close(). f2.bno<<endl.ofstream f3.open("BOOK.DAT".sizeof(B))) { cout<<endl. cin>>x.DAT".sizeof(B)).ios::binary).

<iostream. cout<<" Enter employee number ".h> 2. cin>>eno. gets(ename).(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.h> 5. ifstream 3.h> #include <conio."<<eno<<" "<<" Employee name . char Program: #include <iostream.h> class employee { int eno.h> #include <fstream.h> 4.h> Data Types Used: employee 1. int 4. } int ret() { return(eno). <conio. <process.h> 3."<<ename<<endl. <stdio. <fstream.h> #include <process. ofstream 2. } void putdata() { cout<<" Employee number . char ename[25]. public: void getdata() { cout<<endl.h> #include <stdio. cout<<" Enter employee name ". .

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

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

.

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

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

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

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

f1. } Output: Program 13 Q: Write a program in C++ to create a data file and modify a record without using a 2nd file.read((char*)&S.getdata().seekg(0. } f1.ios::beg).write((char*)&S. while(f1.S.putdata(). Header Files Used: .close().sizeof(S))) S.sizeof(S)). getch(). f1.

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

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

.

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

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

h> 2. <conio.h> #include <fstream. ifstream Program: #include <iostream.h> void main() . int 2. <fstream.h> 3.h> #include <conio. <stdio.h> Data Types Used: 1.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’. <iostream. Header Files Used: 1. ofstream 4.h> 4. char 3.h> #include <stdio.

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

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.h> 4.h> Data Types Used: 1. ifstream Program: . char 3.h> 2. <string. int 2. <conio.h> 3. ofstream 4. <stdio.h> 5. <fstream. Header Files Used: 1. <iostream.

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

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

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

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

} getch(). } Output: .

Sign up to vote on this title
UsefulNot useful