13/03/2016

C++ Program To Read And Display File Line By Line Using File Handling

Problem:- C++ program to read and display text file line by line or c++ program to display the contents of a text file or c++ display text file on screen or c++ program to create a file and display its contents or write a c++ program to display the contents of a file in reverse order or c++ read text file line by line or c++ print contents of file or write a c++ program which copies one file to another
c program to display the contents of a file on the screen or c++ read text file line by line.


Explanation:- First we have to create a file with extension (like .txt) after that by using the program we have to print the file in console screen. Always remember we have to read the file by line by line and also write the file line by line in console screen and one more thing file should be in the same folder in which our program already exists. We can give the file name in console screen so no need to give the filename in a program.


Solution:-

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
  ifstream in;
  char filename[20];

cout<<"Enter The File Name With Extension\n";
cin>>filename;

in.open(filename);

/*Here You Have To Create A File And put some data on it. 
Then Save the with Any Extension With File Name As Above Shown */

  if(!in) 
  {
    cout << "Cannot Open File. Or File Not Find\n";
    return 1;
  }

  char str[1000];

cout<<"\nFile is Given Below\n\n";
cout<<"\n============================================================\n\n";
  
  while(in) 
  {
    in.getline(str, 1000);
    if(in) 
cout << str << endl;
  }

cout<<"\n\n============================================================\n";
  in.close();

  return 0;
}

Output:-

Input:-

C++ Program To Read And Display Text File Line By Line Using File Handling

Output:-

C++ Program To Read And Display Text File Line By Line Using File Handling


You May Like This

1. C Program For Open A File (Or Open The Same Program Itself ) Itself Using File Handling

2. C++ Program To Merge Two File Into Third File Using File Handling

3. C++ Program For Copy One File To Another Using File Handling

4. C++ Program To Display The List Of Current Directory/Folder Using File

5. C++ Program For Count A Character In File Using File

6. C++ Program For Reading A Number From File And Sum Of No. Line By Line Using File


7. C++ Program For Store Employee Information And Display Using Structure

8. C Program For Find A Grade Of Given Marks Using Switch Case

9. C Program For HEAP Sort In Ascending Order Also Show Complexity

10. C++ Program For Selection Sort Using Function
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: