Advertisement



< Prev
Next >



C++ File Input get() Function






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


Before we show you how to use this file input function to read the content of a file present on disk, we must show you how to open a file. To open a file and to perform file input/read operation by using the get() fuction, the C++ language provides us some file stream classes which are discussed in the next section.




File Stream Classes


In order to perform a file input/read operation by 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 the location of a file stored on disk and could also specify a mode in which we want to open this file.





Let's see a specific file mode, which is needed to use the get() function to perform an input/read operation on a file.




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


Let's take a look at a specfic mode which is required to read the content of a file by using the file input get() function.

File Mode Description
ios::in
This mode searches for a file and opens it in the read only mode, and next we could use the get() input function to read the content of a file.






  • The syntax of get() file function


  • The file get() input function allows us to read a file by reading one character at a time out of it. Before we show you how to use this function to read a file, let us see its syntax.

     char get(void)



    Advertisement




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


  • Let's say that we already have a file named File1.txt, with its content :

    File1.txt
    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.



    //C++ Reading the content to a file using ifstream class and file mode ios::in
     
    #include<iostream>
    #include<ifstream>
    
    using namespace std;
    
    int main()
    {
    
    	//Creating an input stream to read a file
    	ifstream ifstream_ob;
    
    	//Opening a file named File1.txt to read its content
    	ifstream_ob.open("File1.txt", ios::in);
    
    	char ch;
    
    	//Reading the file using get() function and displaying its content 
    	while(ifstream_ob)
    	{
    		ch = ifstream_ob.get(); 
    		cout<<ch;
    	}
    
    	//Closing the input strea
    	ifstream_ob.close();
    
    	return 0;
    }

    Output


    
    GendXr : Female
    Age    : 28
    City   : India
    Weight : 60.6 Kg 
    Executing this program will read the content of a file named File1.txt(present 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 a file.




    Please share this article -





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



    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

    C#, PYTHON and DJANGO


    Advertisement