Write a C++ Program to Display Contents of text file

By | 04.01.2017

Display Contents of text file


Write a C++ Program to Display Contents of text file using File Handling. Here’s simple Program to Display the Contents of a text file using File Handling in C++ Programming Language.


Below is the source code for C++ Program to Display Contents of text file using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to Display Contents of text file  */


/*-------------file4.txt Content-----------------

Welcome to CodezClub.
It is a programming Website.
Hope u like it. Thanks.........

------------------------------------------------*/


#include<iostream>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;

int main()
{
        ifstream ifile;
        char s[100], fname[100];

        cout<<"Enter file name to read :: ";
        cin>>fname;

        ifile.open(fname);

        if(!ifile)
        {
                cout<<"\nError in opening file..!!\n";

                exit(0);
        }

        cout<<"\n";

        while(ifile.eof()==0)
    {
        ifile>>s;
        cout<<s<<" ";
    }

        cout<<"\n";
        ifile.close();

return 0;

}

OUTPUT : :


/*  C++ Program to Display Contents of text file  */


/*-------------file4.txt Content-----------------

Welcome to CodezClub.
It is a programming Website.
Hope u like it. Thanks.........

------------------------------------------------*/


******************** OUTPUT *********************

Enter file name to read :: C:\\Users\\acer\\Documents\\file4.txt

Welcome to CodezClub. It is a programming Website. Hope u like it. Thanks.........

Process returned 0

Above is the source code for C++ Program to Display Contents of text file which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.5 4 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments