Program to fetch a particular line from a text file using C++

We all use text files a lot and it’s also very important for us in day to day life. Nowadays, we have many applications which help in searching particular words or contents from a specific line. Have you ever thought of what goes behind these type of applications? Or how are things made so simple using application?
In this article, printing a specific line from a text file using C++ programming will be learned. It follows a basic algorithm and is very simple to implement in other programming languages too which will help you to understand how easily you can read a particular line from a text file in C++.

The complete program is provided at the end of this post.

You can also learn:

Libraries included to fetch particular line from a text file in C++

This program uses only a single header file <stdio.h>. This header file means ‘standard input ouput’, and named as <stdio.h>. The work of this header file in this program is to take input/output from the file saved on the hard disk. This header needs to be used here because the file on which program is implemented is on external memory.

#include<stdio.h>

Concept of the program

Let’s divide the program into three parts for easy understanding.

int line=1;
int found=0;
char liner[100];
int linesearch;
FILE *file;
file=fopen(PATH);


In the first four lines of the program, variable initialization are done. An integer variable ‘line’ is initialized with value ‘1’.  Variable ‘found’ with ‘0’. Character variable ‘liner’ taking the number of characters in a line, and integer variable ‘linesearch’ containing value of line to be searched. Fifth line creates a pointer to locate the file and the sixth line uses ‘fopen’ command to access the file of filename replaced at PATH.

if (file == NULL)
{
  cout<<"file does not exist or doesn't work\n";
  return 0;
}
cout<<"Enter the line to be searched";
cin>>linesearch;
cout<<"Going to line"<<linesearch;

Then the ‘if’ loop is used to print that “file does not exist or doesn’t work” if the pointer points to a location NULL. Later,the user is asked to enter the line number to be printed from the text file. The input taken is stored in the variable ‘linesearch’, and the line number entered by used is printed for user confirmation.

while(fgets(liner,100,file)
{
  if(linesearch==line)
  {
    found=1;
    cout<<"Line "<<linesearch<<"found. Line "<<linesearch<<"says "<<liner;
  }
  if(found==0)
  { 
    cout<<"The line entered is not found";
  }
getch();
}

In this section, while loop is used. The while loop condition uses ‘fgets’ function to get contents of the files. In the while loop it is checked whether the line number entered by the user is valid or not. If valid, the content of the line is printed. If not, the output “the line entered is not found” is printed. getch() function is used to stay on the output screen.

Using this simple algorithm, search system can be made to simplify going through big long files. This algorithm can be used to create search system using other programming languages too.

Complete C++ Program to fetch a particular line

#include<stdio.h>
int main() 
{
int line=1;
int found=0; 
char liner[100]; 
int linesearch; 
FILE *file; 
file=fopen(PATH);
if (file == NULL)
 { 
   cout<<"file does not exist or doesn't work\n";
   return 0;
 } 
cout<<"Enter the line to be searched";
cin>>linesearch;
cout<<"Going to line"<<linesearch;
while(fgets(liner,100,file)
 {
 if(linesearch==line)
 {
   found=1;
   cout<<"Line "<<linesearch<<"found. Line "<<linesearch<<"says "<<liner;
 }
 if(found==0)
 {
   cout<<"The line entered is not found";
 } 
getch();
}

Leave a Reply

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