C%2b%2b Files

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C%2b%2b Files as PDF for free.

More details

  • Words: 815
  • Pages: 8
1.Creating files with Constructor function #include int main() { ofstream fout("data1.txt"); char name[20]; cout<<"Enter Name:"; cin>>name; fout<>branch; fout<>name1; fin>>branch1; cout<<"\n Name: "<>name; fout<>branch; fout<>name1; fin>>branch1; cout<<"\n Name: "<
3.using multiple files simultaneously #include int main() { ifstream fin1,fin2; fin1.open("data1.txt"); fin2.open("data2.txt"); char name1[20],branch1[10]; char name2[20],branch2[10]; fin1>>name1; fin1>>branch1; fin2>>name2; fin2>>branch2; cout<<"\n Name1: "<>name; int l=strlen(name); for(int i=0;i>name; int l=strlen(name); f1.write((char *) &name,sizeof(name)); f1.seekg(0); //go to start f1.read((char *) &name1,sizeof(name1)); cout<<"\n name="<
return 0; } 6. Reading and Writing Class Objects class student { char name[20]; int num; char branch[10]; public: void getdata(void); void putdata(void); }; void student::getdata(void) { cout<<"Enter name: "; cin>>name; cout<<"Enter number: ";cin>>num; cout<<"Enter branch: ";cin>>branch; } void student::putdata(void) { cout<<"\n \t"<
7. Random Access : Class Objects class student { char name[20]; int num; char branch[10]; public: void getdata(void); void putdata(void); }; void student::getdata(void) { cout<<"Enter name: "; cin>>name; cout<<"Enter number: ";cin>>num; cout<<"Enter branch: ";cin>>branch; } void student::putdata(void) { cout<<"\n \t"<>obj;

int loc=(obj-1) * sizeof(s); // Reaching the first byte of the object to be modified if(file.eof()) file.clear(); file.seekp(loc); cout<<"\n Enter new values of objects:\n"; s.getdata(); file.write((char *) & s,sizeof(s)); file.seekg(0); while(file.read((char *) & s,sizeof(s))) s.putdata(); file.close(); return 0; }

Exception Handling 1./* Try Block Throwing Exception */ int main() { int a,b; cout<<"Enter a , b"; cin>>a>>b; try { if(b!=0) cout<<"Division Result = "<
division(10,5); division(3,0); } catch(int i) { cout<<"\n Caught The Exception \n"; } cout<<"End"; return(0); } 3./* Multiple catch statements */ void multi_catch(int x) { try { if(x==1) throw x; else if (x==0) throw 'a'; else if (x==-1) throw 1.5; cout<<"TryBlock"<<endl; } catch(char c) { cout<<"Caught a character"<
multi_catch(1); multi_catch(0); multi_catch(-1); multi_catch(2); cout<<"End"; return(0); } 5./* Catching all exceptions */ void multi_catch(int x) { try { if(x==1) throw x; else if (x==0) throw 'a'; else if (x==-1) throw 1.5; cout<<"TryBlock"<<endl; } catch(int p) { cout<<"Caught An integer "<
division(3,0); } catch(int i) { cout<<"\n Caught inside main \n"; } cout<<"End"; return(0); } 7./* Specifying Exceptions */ void multi_catch(int x) throw(int,float) { if(x==1) throw x; else if (x==0) throw 'a'; else if (x==-1) throw 1.5; cout<<"TryBlock"<<endl; } int main() { clrscr(); try { multi_catch(1); multi_catch(0); multi_catch(-1); multi_catch(2); } catch(int p) { cout<<"\n Caught an integer"<<endl; } catch(char c) { cout<<"\n Caught a character"<<endl; } catch(float q) { cout<<"\n Caught a float"<<endl; } cout<<"End"; return(0); }

Related Documents

Files
November 2019 41
Files
May 2020 27
Files
June 2020 27
Files
November 2019 34
Files
May 2020 25
A-files
May 2020 9