Advertisement



< Prev
Next >



C++ Reading a file on disk




In our last article, we have introduced you to some important file input/output operations we could perform. In this tutorial, we are going to explain how to read the content of an existing file. But before we read the content of an existing file present on the disk, we must open it. For this, the C++ language provides us stream classes used to perform file input/read operations.

In order to perform file input/read operation, C++ provides us a few file stream classes, such as -
These file stream classes provides us a function named open(), using which we could provide location of the file stored on the disk to depending on the mode in which we open this file. Let's read the file mode to use to read the content of a file.









Reading a file


Let's look at the mode required to read a file.

File Mode Description
ios::in
Searches for the file and opens it in read mode only(if the file is found).






  • Using getline() function to read a file


  • We can read the content of a file using an in-built function getline() and this function is available to the stream classes fstream and ifstream. Let's see the syntax of getline() function -

    getline (char* arr, int length);

    The getline() function pulls the characters from the file and stores them into s as a c-string)char array) with the maximum length, until either the extracted character is the delimiting character i.e. a newline character('\n') or the EOF(end of file) is reached.


    Advertisement




  • Reading a file present on disk


  • Let us see a code to search for a particular file in the current directory(the same directory where we are going to store the upcoming C++ program.) Let's say the name of this file is File.txt

    File.txt
    Hello there!
    How are you doing? 
    May you have a blessed day!



    A program to read a file present on disk
    //Reading the content of a file using getline() function.
     
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
    int size = 40;
    char arr[size];
    
    //Creating an input stream to read a file
    ifstream fin;
    
    //Opening a file named File.txt for reading its content
    fin.open("File.txt", ios::in);
    while(fin)
    {
    	fin.getline(arr, size);
    	cout<<arr;
    	cout<<"\n";
    }
    fin.close();
    
    return 0;
    }

    Output


    Hello there!
    How are you doing? 
    May you have a blessed day!


    Program Analysis


    As you may see in the output of the code, the program looks for a file named File.txt in the directory where this C++ program is stored and if the file is found, extracts the characters present in the file and display it at the console.




    Please share this article -





    < Prev
    Next >
    < File and File modes
    Write a File >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON and DJANGO


    Advertisement