Anda di halaman 1dari 8

1.

Creating files with Constructor function


#include<fstream.h>
int main()
{
ofstream fout("data1.txt");
char name[20];
cout<<"Enter Name:";
cin>>name;
fout<<name<<"\n";
char branch[10];
cout<<"Enter Branch:";
cin>>branch;
fout<<branch;
fout.close();
ifstream fin("data1.txt");
char name1[20],branch1[10];
fin>>name1;
fin>>branch1;
cout<<"\n Name: "<<name1<<"\n Branch:"<<branch1<<endl;
getch();
return 0;
}
2.Creating files with open function
int main()
{
ofstream fout;
fout.open("data2.txt");
char name[20];
cout<<"Enter Name:";
cin>>name;
fout<<name<<"\n";
char branch[10];
cout<<"Enter Branch:";
cin>>branch;
fout<<branch;
fout.close();
ifstream fin;
fin.open("data2.txt");
char name1[20],branch1[10];
fin>>name1;
fin>>branch1;
cout<<"\n Name: "<<name1<<"\n Branch:"<<branch1<<endl;
return 0;
}
3.using multiple files simultaneously
#include<fstream.h>
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: "<<name1<<"\n Branch1:"<<branch1<<endl;
cout<<"\n Name2: "<<name2<<"\n Branch2:"<<branch2<<endl;
return 0;
}
4. Input Output operations on characters
int main()
{
fstream f1;
f1.open("data3.txt",ios::in | ios::out);
char name[20];
cout<<"Enter Name:";
cin>>name;
int l=strlen(name);
for(int i=0;i<l;i++)
f1.put(name[i]);
f1.seekg(0); //go to start
char ch;
while(f1) { f1.get(ch); cout<<ch; }
return 0;
}
5. Input Output operations on binary files
int main()
{
fstream f1;
f1.open("data5.txt",ios::in | ios::out);
char name[20],name1[20];
cout<<"Enter Name:";
cin>>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="<<name1;
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"<<name<<"\t"<<num<<"\t"<<branch<<endl;
}
int main()
{
student s[3];
fstream file;
file.open("Student.txt",ios::in | ios::out);
cout<<"Enter Student Details"<<endl;
for(int i=0;i<3;i++)
{
s[i].getdata();
file.write((char *) & s[i],sizeof(s[i]));
}
file.seekg(0);
cout<<"\n\n \t /***Student Details***/"<<endl;
cout<<"\n\tName \tNum \tBranch"<<endl;
for(i=0;i<3;i++)
{
file.read((char *) & s[i],sizeof(s[i]));
s[i].putdata();
}
file.close();
return 0;
}
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"<<name<<"\t"<<num<<"\t"<<branch<<endl;
}
int main()
{
student s;
fstream file;
file.open("Student.txt", ios::in | ios::out | ios::ate|ios::binary);
cout<<"\n\n \t /***Student Details***/"<<endl;
cout<<"\n\tName \tNum \tBranch"<<endl;
file.seekg(0,ios::beg);
while(file.read((char *) & s,sizeof(s)))
s.putdata();
file.clear(); // turn of EOF flag
cout<<"Add Another Student Record \n";
s.getdata();
file.write((char *) & s,sizeof(s));
file.seekg(0,ios::beg);
cout<<"\n Appended File Contents\n";
while(file.read((char *) & s,sizeof(s)))
s.putdata();
int obj_size=sizeof(s);
int file_size=file.tellg();
int no_of_objs=file_size/obj_size;
cout<<"\n File size= "<<file_size<<endl;
cout<<"\n No of objects = "<<no_of_objs<<endl;
int obj;
cout<<"\n Enter Object number to be updated :";
cin>>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 = "<<a/b<<endl;
else
throw(b);
}
catch(int b) { cout<<"Exception Caught : b ="<<b<<endl; }
cout<<"End";
return(0);
}
2./* Invoking Function that generates Exception */
void division(int x,int y)
{
cout<<"\n Inside Function\n";
if(y!=0) { cout<<"Result="<<x/y<<endl; }
else { throw(y); }
}
int main()
{
try
{
cout<<"\nTry Block:\n";
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"<<c<<endl; }
catch(int p) { cout<<"Caught an integer"<<a<<endl; }
catch(float f) { cout<<"Caught a float value"<<f<<endl; }
cout<<"End of Try Catch Block"<<endl;
}
int main()
{
multi_catch(1);
multi_catch(0);
multi_catch(-1);
multi_catch(2);
cout<<"End";
return(0);
}
4./* 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(...) { cout<<"Caught an Exception"<<endl; }
cout<<"End of Try Catch Block"<<endl;
}
int main()
{
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 "<<p<<endl; }
catch(...) { cout<<"Caught an Exception"<<endl; }
cout<<"End of Try Catch Block"<<endl;
}
int main()
{
multi_catch(1);
multi_catch(0);
multi_catch(-1);
multi_catch(2);
cout<<"End";
return(0);
}
// catch(...) should always be placed last in the list of handlers. Placing it before other
catch blocks prevents those blocks from catching exceptions
6./* Rethrowing An Exception */
void division(int x,int y)
{
cout<<"\n Inside Function\n";
if(y!=0){ cout<<"Result="<<x/y<<endl;}
else{throw(y);}
catch(int m){ cout<<"Caught divisor value as 0 inside the function"<<endl; throw; }
}
int main()
{
try
{
cout<<"\nTry Block:\n";
division(10,5);
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);
}

Anda mungkin juga menyukai