Unit –V (Streams
Computation with Files)
By Dr. AMAN TYAGI
Department of Computer Science and Engineering
Read/Write Class Objects from/to
File in C++
▪ The iostream standard library has two methods cin, to accept input
from standard input stream and cout to print output to the standard
output stream. In this lecture we will learn how to read data from files
into class objects and how to write data in class objects to files.
▪ Reading and writing data to and from files requires another standard
library of C++ <fstream>. The three main data types of fstream are −
▪ ifstream − represents input file stream and reads information from
files.
▪ ofstream − represents output file stream and writes information to
files.
▪ fstream − represents general file stream and has capabilities of both.
getline (string) in C++
▪ The C++ getline() is a standard library function that is used to read a
string or a line from an input stream. It is a part of the <string> header.
The getline() function extracts characters from the input stream and
appends it to the string object until the delimiting character is
encountered. While doing so the previously stored value in the string
object str will be replaced by the input string if any.
▪ //C++ program to write and read object using read and write function. Writing and
▪ #include <iostream>
Reading class
▪ #include <fstream>
objects
▪ using namespace std;
▪ class student //class student to read and write student details
▪ {
▪ private:
▪ char name[30];
▪ int age;
▪ public:
▪ void getData(void)
▪ { cout<<"Enter name:"; cin.getline(name,30);
▪ cout<<"Enter age:"; cin>>age;
▪ }
▪ void showData(void)
▪ {
▪ cout<<"Name:"<<name<<",Age:"<<age<<endl;
▪ }
▪ };
▪ int main()
▪ {
▪
student s;
▪ ofstream file;
▪ file.open("aaa.txt",ios::out); //open file in write mode
▪ if(!file)
▪ {
▪ cout<<"Error in creating file.."<<endl;
▪ return 0;
▪ }
▪ cout<<"\nFile created successfully."<<endl;
▪ s.getData(); //read from user //write into file
▪ file.write((char*)&s,sizeof(s)); //write into file
▪ file.close(); //close the file
▪ cout<<"\nFile saved and closed succesfully."<<endl;
▪ ifstream file1; //re open file in input mode and read data //open file1
▪ file1.open("aaa.txt",ios::in); //again open file in read mode
▪ if(!file1){
▪ cout<<"Error in opening file..";
▪ return 0;
▪ }
▪ //read data from file
▪ file1.read((char*)&s,sizeof(s));
▪ //display data on monitor
▪ s.showData();
▪ //close the file
▪ file1.close();
▪ return 0;
▪ }
Output
Create and Write To a File
▪ #include <iostream>
▪ #include <fstream>
▪ using namespace std;
▪ int main() {
▪ // Create and open a text file
▪ ofstream MyFile("filename.txt");
▪ // Write to the file
▪ MyFile << "Files can be tricky, but it is fun enough!";
▪ // Close the file
▪ MyFile.close();
▪ }
OUTPUT
OUTPUT TO OPEN THE FILE
Input/output with files
▪ C++ provides the following classes to perform output and input of
characters to/from files:
▪ ofstream: Stream class to write on files
▪ ifstream: Stream class to read from files
▪ fstream: Stream class to both read and write from/to files.
▪ // writing on a text file Example write
▪ #include
<iostream>
▪ #include <fstream>
▪ using namespace std;
▪ int main () {
▪ ofstream myfile ("example.txt");
▪ if (myfile.is_open())
▪ {
▪ myfile << "This is a line.\n";
▪ myfile << "This is another line.\n";
▪ myfile.close();
▪ }
▪ else cout << "Unable to open file";
▪ return 0;
▪ }
Example Read
▪ // reading a text file
▪ #include <iostream>
▪ #include <fstream>
▪ #include <string>
▪ using namespace std;
▪ int main () {
▪ string line;
▪ ifstream myfile ("example.txt");
▪ if (myfile.is_open())
▪ {
▪ while ( getline (myfile,line) )
▪ {
▪ cout << line << '\n';
▪ }
▪ myfile.close();
▪ }
▪ else cout << "Unable to open file";
▪ return 0;
▪ }
Output of both program
END