This document contains code and text related to file handling in C++. It includes code for handling binary data files using classes and reading/writing data to a binary file. It also includes code for handling text files, reading a string from the user and writing it to a text file. The document provides examples of reading and writing data to both binary and text files in C++.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
117 views66 pages
Computer Science Practical File Class 12
This document contains code and text related to file handling in C++. It includes code for handling binary data files using classes and reading/writing data to a binary file. It also includes code for handling text files, reading a string from the user and writing it to a text file. The document provides examples of reading and writing data to both binary and text files in C++.
This is hereby to certify that, the original and genuine project
work has been
carried out and completed solely, sincerely and satisfactorily
by Khush Malik, a student
of class 12th C under the roll number __________________ for
the academic session
2019 - 20 for the Computer Science department under the direct
supervision of the
undersigned as per the requirement for the Board Examination.
_________________________ Teacher in-charge vfAcknowledgment.d The successful completion of any task would be incomplete without mentioning the names of those persons who helped to make it possible. I take this opportunity to express my gratitude in few words and respect to all those who helped me in the completion of this project.
It is my humble pleasure to acknowledge my deep sense
of gratitude to my Computer Science teacher, Mr. Amit Dua for her valuable support, constant help and guidance at each and every stage, without which this project would not have come forth. I would also like to thank my friends and family for encouraging me during the course of this project.
Last but not the least, I would like to thank CBSE
for giving me the opportunity to undertake this project. aINDEX ++v S. No TOPIC Signature
1 Number Conversion
2 Searching 3 Sorting 4 2-D Array address mapping 5 Data file handling (Binary) 6 Data file handling (Text) 7 Classes (Inline and offline functions)
8 Classes (Friends) 9 Classes(Inheritance) 10 Classes (constructor and destructor) 11 Classes (Feature of OOPS) 12 This pointer 13 Boolean Algebra 14 Circular queue 15 2-D Array 16 String 17 Switch Case (Menu driven program) 18 Loops (For, While and Do While) 19 Queue Code 20 Postfix to infix 21 SQL d<NumberConversion.cpp>s
void main() { clrscr(); gotoxy(30,1); cout<<"Arrays\n"; cout<<"There are two types of arrays\n"; cout<<"1D array\n"; cout<<"There are 2 types of searches \n"; cout<<"1. linear search 2. binary search\n"; cout<<"2D array\n"; int ch; cin>>ch; switch(ch) { case 1: int a[20],size,flag=0,i,num,pos; cout<<"\nEnter the number of elements in the array:"; cin>>size; cout<<"\nEnter the elements of array(in ascending order):"; for(i=0;i<size;i++) cin>>a[i]; cout<<"\nEnter the element to be searched"; cin>>num; for(i=0;i<size;i++) if(a[i]==num) { flag=1; pos=i; break; } if(flag==0) cout<<"\n Element not found"; else cout<<"\nElement found at position"<<(pos+1); break;
case 2: cout<<"\nBinary search\n";
int f[8]={1,2,3,4,5,6,7,8}; int z=0,l=0,u=7,s,m; cout<<"Enter element to be searched"; cin>>s; while(l<=u) { z=1; m=(l+u)/2; if(f[m]==s) { cout<<"\nElement found at"<<m; } else if(f[m]>s) u--; else l++; } if(z==0) cout<<"not found"; break;
void main() { clrscr(); int opt,base,storage,n,m,addr,i,j; cout<<"1. column major"<<endl; cout<<"2. row major"<<endl; cin>>opt; if(opt==1) { cout<<"enter the location"<<endl; cin>>i>>j; cout<<"enter the base address"<<endl; cin>>base; cout<<"enter the size of element"<<endl; cin>>storage; cout<<"enter the number of rows"<<endl; cin>>m; cout<<"enter the number of columns"<<endl; cin>>n; cout<<endl; addr=base+storage*(n*i+j); } else if(opt==2) { cout<<"enter the location"<<endl; cin>>i>>j; cout<<"enter the base address"<<endl; cin>>base; cout<<"enter the size of element"<<endl; cin>>storage; cout<<"enter the number of rows"<<endl; cin>>n; cout<<"enter the number of columns"<<endl; cin>>m; cout<<endl; addr=base+storage*(n*j+i); } else cout<<"invalid input"; cout<<"you addr is"<<addr; getch(); } OUTPUT
class taxi { int taxinumber; float distance; char startlocation[20],destination[20]; public: void allocate() { cout<<"Enter the taxi number: "; cin>>taxinumber; cout<<"Enter the place from where the taxi is boarded: "; cin>>startlocation; cout<<"Enter the distance: "; cin>>distance; cout<<"Enter the place where the taxi is to be deboarded: "; cin>>destination; } ~taxi() { cout<<"\n\n\n\n\t\tTHANK YOU"; } void show(); }; void taxi::show() { cout<<"\n\n\t\tDetails of the taxi are-"<<"\ntaxi number is: "<<taxinumber<<"\nPlace o]where taxi will be boarded: "<<startlocation<<"\nDistance between the places: "<<distance; };
class data1 { int a,b; public: void get1(); friend void sum(data1,data2); }; class data2 { int c,d; public; void get2(); class display { int s; public: void sum(int a, int b) { s=a+b; } void show() { cout<<"\nSum of a and b is:: "<<s; } }; friend void sum(data1,data2); }; void sum(data1 a1,data2 a2) { cout<<"a1.a+a2.c"<<a1.a+a2.c<<endl; cout<<"a1.b+a2.d"<<a1.b+a2.d<<endl; } void data1::get1() { cout<<"Enter value for a: "; cin>>a; cout<<"Enter value for b: "; cin>>b; } void data2::get2() { cout<<"Enter value for c: "; cin>>c; cout<<"Enter value for d: "; cin>>d; }
void main() { clrscr(); data1 a; data2 b; a.get1(); b.get2(); sum(a,b); data2::display x; cout<<"ENTER THE VALUE IN P AND Q :"; int p,q; cin>>p>>q; x.sum(p,q); x.show(); getch(); }
void main() { clrscr(); cout<<"Enter the number\n"; int a[3][3],c,r; for(r=0;r<3;r++) for(c=0;c<3;c++) { cin>>a[r][c]; } cout<<"1. The general array\n"; cout<<"2. To show diagonals\n"; cout<<"3. Formation of triangle and the sum of numbers in triangls\n"; cout<<"Enter the option\n"; int ch; cin>>ch; switch(ch) { case 1: cout<<"The numbers are\n"; for(r=0;r<3;r++) //to show all numbers present for(c=0;c<3;c++) cout<<a[r][c]<<endl; break; case 2: cout<<"The numbers are\n"; for(r=0;r<3;r++) //to show numbers present in diagonals for(c=0;c<3;c++) //(0,0) (0,2) (1,1) (1,0) (2,0) if(r==c||r+c==2) cout<<a[r][c]<<endl; break; case 3: cout<<"The sum of the numbers are\n"; int sum=0; //to show numbers forming a triangle for(r=0;r<3;r++) //(,) (,) (,) (,) (,) for(c=0;c<3;c++) if(r<=c) sum= sum + a[r][c]; cout<<sum; break; } getch(); OUTPUT
void main() { clrscr(); char a[100],b[100],c[100]; cout<<"Enter the string\n"; gets(a); int ch,i,j,c=0,temp=0; cout<<"Enter the option\n"; cin>>ch; switch(ch) { case 1: cout<<"The general way of entering a string\n"; cout<<a; break; case 2: cout<<"To determine the string length\n"; cout<<strlen(a); break; case 3: cout<<"To find number of words\n"; for(i=0;a[i]!='\0';i++) if(a[i]=' ') c++; cout<<c; break; case 4:cout<<"To copy the string from 1st array to the second\n"; for(i=0;a[i]!='\0';i++) temp=a[i]; a[i]=b[i]; b[i]=temp; cout<<b; break; } getch(); }
OUTPUT d<Switch.cpp>s #include<iostream.h> #include<conio.h> #include<iomanip.h> #include<math.h> #include<process.h> void main() { clrscr(); cout<<"Choose one of the following questions\n"; cout<<"1. Question 1 - Age of voting\n"; cout<<"2. Question 2 - Greater number\n"; cout<<"3. Question 3 - exponetial power\n"; cout<<"4. Question 4 - square root of number\n"; cout<<"5. Exit\n"; int ch; cin>>ch; switch(ch) { case 1 : cout<<"Eligibility for voting\n"; cout<<"Enter your age\n"; int x; cin>>x; { if (x>=18) cout<<"You can vote"; else cout<<"You cannot vote"; } break; case 2 : cout<<"Greater number\n"; int a,b; cout<<"Enter 1st number"; cin>>a; cout<<"Enter 2nd number"; cin>>b; { if (a>=b) cout<<"1st number is greater than or equal to 2nd"; else cout<<"2nd number is greater than 1st"; break; } case 3 : cout<<"Exponenetial power\n"; int p,q,r; cout<<"Enter the base number\n"; cin>>p; cout<<"Enter the power the number is to be raised\n"; cin>>q; r= pow(p,q); cout<<"The answer is =\n"; cout<<r; break; case 4 : cout<<"Square root of a number\n"; int l,m; cout<<"Enter number\n"; cin>>l; m=sqrt(l); cout<<"Square root of the number is="; cout<<m; break; case 5 : cout<<"Exit"; exit (0); break; } getch(); }
void main() { clrscr(); gotoxy(30,1); cout<<"Loops\n"; cout<<"There are 3 types of loops\n"; cout<<"1. For loop \t\t 2. While loop \t\t 3. Do While loop\n"; cout<<"choose one of the following type to see an example\n"; int ch; cin>>ch; switch(ch) { case 1 : cout<<"Example of for loop\n"; cout<<"numbers from 1-9\n"; int a; cout<<" for(a=1;a<10;a++) "; cout<<"{cout<<a;}"; delay(1000); for(a=1;a<10;a++) { cout<<a; } break;