codescracker
c++

C++ Program to Read and Display File's Content



« Previous Program Next Program »


In this article, you will learn and get code to read and display file's content using a C++ program. But before going through program, let's first create a text file, codescracker.txt

Things to Do Before Program

Since the program given below read and displays the content of a file entered by user. Therefore, we must have to create a file named codescracker.txt with following content:

Hello C++
I'm a File
My Name is codescracker.txt

Save the file with this content, inside the current directory. The current directory means, the directory where your C++ source code to read and display file's content is saved. Here is the snapshot of opened codescracker.txt file:

read and display file content c++

And here is the folder, cpp programs where the file is saved. In this folder, I'll also save the C++ source code given below to read and display file's content:

c++ read display file content

Now let's move on and create a C++ program to read the content of file, codescracker.txt

Read and Display File's Content in C++

To read and display file's content in C++ programming, you have to ask from user to enter the name of file along with its extension say codescracker.txt.

Now open the file using open() function. And then read its content in character-by-character manner. Display the content (character by character) at the time of reading as shown here in the following program:

The question is, write a program in C++ to read and display the content of a file entered by user at run-time. Here is its answer:

// C++ Program to Read and Display File's Content
// ----codescracker.com----

#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
    char fileName[30], ch;
    fstream fp;
    cout<<"Enter the Name of File: ";
    gets(fileName);
    fp.open(fileName, fstream::in);
    if(!fp)
    {
        cout<<"\nError Occurred!";
        return 0;
    }
    cout<<"\nContent of "<<fileName<<":-\n";
    while(fp>>noskipws>>ch)
        cout<<ch;
    fp.close();
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Before executing the code, be sure to save this C++ source code in the same folder where you've created or saved your codescracker.txt file. Here is the initial output produced by this program:

C++ program read and display file content

Now supply the name of newly created file, codescracker.txt and press ENTER to read its content and display it as shown in the snapshot given below:

c++ display file content

The fstream type variable allows to work with files in C++. It is defined in fstream header file.

The function used in above program, that is open() receives one/two argument(s). The first argument is compulsory, that is the name of file to open. Whereas the second of its argument is optional, that indicates to its opening mode.

The fstream::in file opening mode is used to open the file to read its content only. And the following statement:

fp>>noskipws>>ch

reads data from file in character-by-character manner, without skipping any white spaces.

You can also use the following block of code to read and display file's content:

while(!fp.eof())
{
    fp.get(ch);
    cout<<ch;
}

instead of:

while(fp>>noskipws>>ch)
    cout<<ch;

But still if you want the complete program, then here it is, with only two extra changes. That is the condition of both if and while loop:

// ----codescracker.com----

#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
    char fileName[30], ch;
    fstream fp;
    cout<<"Enter the Name of File: ";
    gets(fileName);
    fp.open(fileName, fstream::in);
    if(fp==NULL)
    {
        cout<<"\nError Occurred!";
        return 0;
    }
    cout<<"\nContent of "<<fileName<<":-\n";
    while(fp.eof()==0)
    {
        fp.get(ch);
        cout<<ch;
    }
    fp.close();
    cout<<endl;
    return 0;
}

This program does the same job as of previous program. That is, asks from user to enter the name of any text file to read and display its content on the screen.

Same Program in Other Languages


« Previous Program Next Program »