Read and display a text file line by line. : File Utility « File « C++






Read and display a text file line by line.

 


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

int main(int argc, char *argv[])
{
  if(argc != 2) {
    cout << "Usage: Display <filename>\n";
    return 1;
  }

  ifstream in(argv[ 1 ]);                 // input

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[ 255 ];

  while(in) {
    in.getline(str, 255);                // delim defaults to '\n'
    if(in) 
       cout << str << endl;
  }

  in.close();

  return 0;
}


           
         
  








Related examples in the same category

1.Display contents of specified file in both ASCII and in hex.
2.Create a file comparision utility.
3.Copy a text file and display number of chars copied.
4.Word count for input file: file read with ifstream
5.Copy files
6.Copy a file and display number of chars copied.
7.Concatenate files
8.Swap characters in a file.
9.Swap characters in a file with error checking.
10.Copy a file and reverse case of letters.
11.Count letters.
12.Copy a file and reverse case of letters with error checking.
13.Copy and convert tabs to spaces.
14.Search file.