What is Stream in c++ and File InputStream and Output stream with c++

Published January 20, 2022

Stream is a flow of characters into or out to a program. In c++ we will use Stream class to make input and output operations.The Stream class as different classes to handle input and output data, The base class for the Stream is ios class
ifstream: This class represents the input stream data and will be used to read from the files
ofstream: This class represents the output stream data and will be used to write into the files

 

Cpp Stream - Input and output streams


Some important point about I/O

  • Input Stream is a flow of data into the program
  • Output Stream is a flow of data from the program
  • cin is the predefined input stream in stream class
  • we will use <iostream> header
  • similarly cout is the predefined output stream is stream class
  • There is an other predefined output stream call cerr, which will used to send all error messages, its similarly like cout
  • Similarly we will have File input stream and File Output stream which will be same like cin and cout but will do the functions on the files
  • To work with File Stream we should use <fstream> header files


Open a File in C++
To read or write any file first we need to open that file. To open the file ofstream or fstream will be used.

Syntax for open a file

void open(const char *filename, ios::openmode mode);

 

In the above method first argument will the file location path and file name and second argument for the type of fileopen mode.
This mode will be
ios::app => This mode is for append the output at the end
ios::ate => This mode is for open a file output and control to the end of the file
ios::in => This mode is for open the file to read the content.
ios::out => This mode is for open the file to write the content.
ios::trunc => If the file already opened then its content will be truncated before open the current file


When we open a file we can apply 1 or more modes by adding OR condition

Example

ofstream outfile;
outfile.open("file.text", ios::out | ios::trunc );

 

How do i close a File in C++?

Normally when we terminate c++ program it automatically flushes all streams(input/output), then release allocated memory and close all the open files. But its a good practice the close the files manually by using close function.

Syntax

void close();

 

A simple c++ example on File Input and File output stream to read and write data on/from the files

#include <fstream>
#include <iostream>
using namespace std;
 
int main () {
   char data[200];

   // open a file in write mode.
   ofstream outfile;
   outfile.open("test.text");

   cout << "Writing to the file" << endl;
   cout << "Enter First text: ";
   cin.getline(data, 200);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Enter your Lucky Number ";
   cin >> data;
   cin.ignore();
   
   // again write inputted data into the file.
   outfile << data << endl;

   // close the opened file.
   outfile.close();

   // open a file in read mode.
   ifstream infile;
   infile.open("test.text");
 
   cout << "Reading from the file" << endl;
   infile >> data;

   // write the data at the screen.
   cout << data << endl;
   
   // again read the data from the file and display it.
   infile >> data;
   cout << data << endl;

   // close the opened file.
   infile.close();

   return 0;
}

 

Conclusion: In this C++ article we covered what is stream and how to read and write files in cpp.

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

141 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions