< Prev
Next >

C++ File Input get() Function

In our last article, we have explained how to append new data to the end of a file on the disk. In this tutorial, we are going to explain how to use the a very important file input function -

Before we show you how to use this file input function to read the content of a file present on the disk, we must show you how to open the file. For this, the C++ language provides us stream classes used to perform file input/read operation.

File Stream Classes

In order to perform a file input/read operation using the get() function, C++ provides us a few file stream classes, such as -
These two 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 see the file mode that we require to use, in order to use the get() function to perform input/read operation on a file.

Mode to perform the file input operation using get() function.

Let's look at the combination of modes required to read content of a file using the get() function.

File Mode Description
Searches for the file and opens it in read mode only and then we could use the get() input function to read the contents of a file.

  • The syntax of get() file function

  • The get function allows us to read one character at a time out of the file being read. Let's see the syntax of get() file input function, before we use it to read the content of a file.

     char get(void)


  • Using the get() function to perform the file input operation.

  • Let's say that we already have an existing file on disk, named File1.txt, with content :

    Gender : Female
    Age    : 28
    City   : India
    Weight : 60.6 Kg

    Now let's try to read the content of this file by using the get() file input function.

    //Reading the content to a file using ifstream class and modes ios::in
    using namespace std;
    int main()
    //Creating an output stream to append new data to a file
    ifstream ifstream_ob;
    //Opening a file named File1.txt to read its content"File1.txt", ios::in);
    char ch;
    //Reading the file using get() function and displaying its content 
    ch = ifstream_ob.get(); 
    return 0;


    GendXr : Female
    Age    : 28
    City   : India
    Weight : 60.6 Kg 
    Executing this program will read the content of a file named File1.txt in the current directory, by using the get() function.

    In the next tutorial, we are going to explain an output put() function, which is used to write a character to the file.

    Please share this article -

    < Prev
    Next >
    < C++ Append to a File
    File Output Function - put() >


    Please Subscribe

    Please subscribe to our social media channels for daily updates.

    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



    Please check our latest addition