Merging cin and file streams.


R. Clayton (rclayton@clayton.cs.monmouth.edu)
(no date)


Here's some code showing how you can treat cin and file streams identically:

#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

static void copy(istream & in) {

  string s;

  while (getline(in, s))
    cout << s << "\n";
  }

int main(int argc, char * argv[]) {

  if (argc == 2) {
    ifstream in(argv[1]);
    copy(in);
    in.close();
    }
  else
    copy(cin);

  return EXIT_SUCCESS;
  }



This archive was generated by hypermail 2.0b3 on Thu Aug 02 2001 - 12:45:05 EDT