Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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;
}

 

Input:

Output:

4 5 1 51 15 151 5 -1

2 2 2 2 2 5 4 4 - 1

3 1 4 8 8 4 8 -1

1 4 4 8 -1 

9 8 7 6 5 4 2 1 -1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program For Find The Sum of Integers In a File... >>