Mega Code Archive

 
Categories / C++ / File
 

Use while loop to read file content

#include <iostream>     #include <fstream>    using namespace std;      main(int argc, char *argv[])    {      char ch;            if(argc!=2) {        cout << "Usage: PR <filename>\n";        return 1;      }            ifstream in(argv[1]);      if(!in) {        cout << "Cannot open file";        return 1;      }            while(in) { // in will be 0 when eof is reached        in.get(ch);        cout << ch;      }            return 0;    }