Home > OOP > Filing in C++

Filing in C++

C++ allows to read and write to console using cout and cin from standard library. This data is only available unless you close the console scree. When you close the console screen you have no way to get or view this data again. What If we want to save data for future use? There is another library available in C++ to store and retrieve data.
C++ allows us to store and retrieve data into the files through a program. This is known as filing in C++. fstream is a c++ library which allows us to do filing with our program. fstream should be included in program in order to read and write data from/to a file.
fstream provides three data types:

  1. ofstream
  2. ifstream
  3. fstream

ofstream: –
ofstream allows us to output to a file i.e. write to a file. So, whenever we need to write something to a file through our program it is necessary to use ofstream in it.
ifstream: –
It allows us to take input from a file. i.e. to read from a file.
fstream: –
this represents file system in general and has capabilities of both ofstream and ifstream. i.e. it allows to both read and to to a file.

To read or write to a file we must open the file using open() function from fstream library. It is part of all three data types of library fstream. We can use open function as:


ofstream of;
of.open (filename, ios::openmode mode);

ifstream if;
if.open (filename, ios::openmode mode);

fstream f;
f.open (filename, ios::openmode mode);

openmode can be one of the following telling compiler to open file in which mode.
    1. ios::app

Append mode. It will append new output at the end of the contents already stored in file.

    1. ios::ate

Open a file for output and move the read/write control to the end of the file.

    1. ios::in

Opens file for reading.

    1. ios::out

Opens file for writing.

    1. ios::trunc

If the file already exists, it will truncate the file before opening it.

We must close the file after completing the required task. We use close() for this purpose. Like open() It is also available in all three data types of fstream library.

Read & write to file

We can use insertion operator(<<) to write data to a file. Insertion operator with ofstream object is used exactly the way we did with cout.
E.g.

     string data;
     data = “This is a test to write a file”;
     ofstream of;
     of.open(file-Name, ios::out);
     of << data;

Similarly, data can be read from file with extraction operator (>>). It can also be used with ifstream exactly as we used it with cin.
E.g.

     string data;
     ifstream if;
     if.open(file-Name, ios::in);
     if>>data;

Both insertion and extraction operators can be used with fstream object.
E.g.

     string data;
     data = “This is a test to write a file”;
     fstream f;
     f.open (file-Name, ios::out,|ios::in);
     f << data;
     f>>data;

Example program: –

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string"

using namespace std;

class employee 
{
     int id;
     string name;

     public:
          int getId()
          {
               return id;
          }
          string getName()
          {
               return name;
          }
          void addEmp() 
          {
               cout << "Enter ID : ";
               cin >> id;
               cin.ignore();
               cout << "Enter Name : ";
               getline(cin, name);
          }
          void writeToFile() 
          {
               addEmp();
               ofstream out;
               out.open("employee.dat", ios::out);
               out << getId();
               out << endl;
               out << getName();
               out.close();
          }
          void readFile()
          {
               int id;
               string name;
               ifstream in;
               in.open("employee.dat", ios::binary);
               in >> id;
               in >> name;
               in.close();
               cout << "Id : " << id << endl;
               cout << "Name : " << name << endl;
          }
};
int main()
{
     employee emp;
     cout << "***Writing To File***\n";
     emp.writeToFile();
     cout << "\n***Reading From File***\n";
     emp.readFile();
     return 0;
}

Output: –

Filing in c++ example

Filing Using Read and Write Functions

Filing can also be done using read and write functions of data types provided by fstream library.
Below example will explain working of read and write functions and how we can read and write data using these functions.
Example program: –

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string"

using namespace std;

class employee 
{
     int id;
     string name;

     public:
          void addEmp() 
          {
               cout << "Enter ID : ";
               cin >> id;
               cin.ignore();
               cout << "Enter Name : ";
               getline(cin, name);
          }
          void writeToFile() 
          {
               addEmp();
               ofstream out;
               out.open("employees.dat", ios::app | ios::binary);
               out.write(reinterpret_cast<char*>(this), sizeof(this));
               out.close();
          }
          void readFile()
          {
               ifstream in;
               in.open("employees.dat", ios::binary);
               in.read(reinterpret_cast<char*>(this), sizeof(this));
               in.close();
               this->showData();
          }
          void showData()
          {
               cout << "Id : " << id << endl;
               cout << "Name : " << name << endl;
          }
};
int main()
{
     employee emp;
     cout << "***Writing To File***\n";
     emp.writeToFile();
     cout << "\n***Reading From File***\n";
     emp.readFile();
     return 0;
}

Output: –

Filing using read and write functions - How to Read and write to File in C++?

Leave a Reply

Your email address will not be published. Required fields are marked *