C++ program to create a text file, open and read a particular line

In this tutorial, we will learn about how to Create a text file, how to open a text file and how to read a particular line from a text file in C++.
before we create a file, do you know Why we use the concept of files in C++?
Files are called a storage device which means to store the output of the program so that we can use that data in the future.

File Handling Classes

  1. ifstream:  This class provides input operation and use to read the information from Files.
  2. ofstream: This class provides output operation and use to create files and write the information to the files.
  3. fstream: This class provides both input and output operations and use to create files, write information to files, and read information from files.

These classes are derived from fstreambase and from corresponding iostream class.

Create a text file in C++

#include <iostream>
#include <fstream>
using namespace std;
int main () 
{
  ofstream file;
  file.open ("Codespeedy.txt");
  if (file.is_open())
  {
  file << "hello i am Raj gaurav.\n";
  file << "hello i am Raj gaurav2.\n";
  file.close();
  }
  else cout << "can not open a file";
  return 0;
  }


  1. open(): It is used to create and open a file
  2. close(): It is used to close an existing file
  3. read(): It is used to read data from a file
  4. write(): It is used to write data into a file
  5. get(): It is used to read a single character from the file
  6. put(): It is used to write a single character in the file

How to open a File in C++

A file must be open before doing any operation on it.
A file can be open in two ways

By using Constructor function

ifstream file("Codespeedy.txt");

ofstream file("Codespeedy.txt");

By using member function open()

Syntax: Stream-object.open(“filename”, mode)




ifstream file;
file.open("Codespeedy.txt");

ofstream file;
file.open("Codespeedy.txt");

Note:  These function can take two arguments, the second one for specifying the file mode.

Some File mode parameters

  1. ios::app :  Append mode (Append to end of file)
  2. ios::ate  :  go to End of file on opening
  3. ios::binary : open file in binary mode
  4. ios::in :  open file for reading only
  5. ios::out :  open file for writing only
  6. ios::trunc :  If the file already exists, Its contents will be truncated before file opening

How to read a particular line from a text file in C++

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  string line;
  ifstream file ("Codespeedy.txt");
  if (file.is_open())
  {
    for (int lineno = 1; getline (file,line) && lineno < 3; lineno++)
      if (lineno == 2)
          cout << line << endl;
    file.close();
  }
 else cout << "Can not open the file"; 
 return 0;
}

 

According to the above piece of code, we include one more class which is called string, which will help us to use strings. we declare a string variable to store the line which we are going to read. the ifstream is used to read the information from files. the if ( file.is_open ) is used to check if the file is open or not, and this method will return a boolean value. if the file is open, it will return true otherwise it returns false. we use  For loop to read a particular line from the file and then print that line.

The output will be: i am Raj gaurav2.

Read more tutorials:


Leave a Reply

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