Chapter 6. Exception Handling and Stream I - O
Chapter 6. Exception Handling and Stream I - O
Chapter 6. Exception Handling and Stream I - O
char
int
400;
Example:
fstream new_file;
new_file.open(“newfile.txt”, ios::out);
• Using a stream insertion operator << we can write
information to a file
• Using stream extraction operator >> we can easily
read information from a file.
Example of opening/creating a file using the open() function
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
new_file.close();
}
return 0;
}
Writing to a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file_write.txt",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
new_file<<"Learning File handling"; //Writing to file
new_file.close();
}
return 0;
}
Reading from a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file_write.txt",ios::in);
if(!new_file)
cout<<"No such file"; } else { char ch; while (!new_file.eof()) { new_file
>>ch;
cout << ch;
}
new_file.close();
return 0;
}
Close a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file.txt",ios::out);
new_file.close();
return 0;
}
Practise:
• Create a file “Welcome.txt” and “write
welcome to file handling in C plus plus” and
then read the file content.
Reading and Writing Object
• read data from files into class objects
• write data in class objects to files.
Writing class object to the file
class_name obj;
ofstream file1;
file1.open( “file_name”, ios::app );
file1.write( (char*)&obj, sizeof(obj) );
Here,
• data present in class object obj is written to file
specified file by calling write function.
• (char*)&obj is used to point at the start of an object.
• sizeof(obj) calculates the number of bytes copied in
file.
Reading class object to the file
class_name obj2;
ifstream file2;
file2.open( “file_name”, ios::in );
file2.read( (char*)&obj2, sizeof(obj2) );
Here,
• data present in class object obj2 is read from file
specified by calling read function.
• (char*)&obj2 is used to point at the start of an object
and
• sizeof(obj2) calculates the number of bytes to read
from the file.
#include <iostream>
#include <fstream>
#include<string.h>
using namespace std;
class Student {
public:
char Name[20];
int Student_ID;
int age;
};
int main(){
Student std;
Strcpy(std.Name, "John”);
std.Student_ID=2121;
std.age=11000;
ofstream file1;
file1.open(” Details.txt”, ios::out);
for(i=0;i<2;i++)
{
p[i].get_info();
file1.write((char*)&e[i],sizeof(e[i]));
}
file1.close();
Ifstream file2;
file2.open(”Details.txt",ios::in);
file2.seekg(0);
cout<<“Name\t”<<“ Occupation\t” <<“ age”<<endl;
for(i=0;i<2;i++)
{
file2.read((char*)&e[i],sizeof(e[i]));
cout<<“……………………………………”;
p[i].set_info();
}
file2.close();
return 0;
}
• Create a class called book having a member
name, price, author and pages. Create a file
called ‘’Library.docx” and store record of 500
books. Now, read the file to print the
information of the book which is above 300.
• A class called ”student" has the following
attributes: roll number, name , age , and
address . Implement the necessary
functions to allow the user to display for
100 student objects from file
’StudentInfo.txt’.
#include <iostream>
#include <fstream>
#include<string.h>
using namespace std;
class Student {
private:
char Name[20];
char add[50];
int age;
Int roll;
public:
void set_info()
{
Cout<<Name<<“\t”<<add<<“\t”<< age<<“\t” <<roll<<endl;
}
};
int main(){
int i;
Student e[100];
Ifstream file2;
file2.open(”studentInfo.txt",ios::in);
if(!new_file)
{
cout<<"No such file";
}
else
{
file2.seekg(0);
cout<<“Name\t”<<“ address\t” <<“ age”<<“\t”<<roll<<endl;
for(i=0;i<100;i++)
{
file2.read((char*)&e[i],sizeof(e[i]));
cout<<“……………………………………”;
p[i].set_info();
}
}
file2.close();
return 0;
}
End of chapter 6