[LC++]choosing between cin and file input at runtime; how?

Mark Phillips mark at austrics.com.au
Tue Nov 13 17:21:05 UTC 2001


I want my program to specify an input file via the commandline,
with an entry of "-" indicating that input should be from standard
input.

What I have done is created an istream object, and assigned it to
either cin, or to a filebuf object, depending on what was specified
at the commandline.  Here's the code:

  filebuf inputFb;
  istream is;
  if (inputFileName=="-")
    is.rdbuf(cin.rdbuf());
  else if (inputFb.open(inputFileName.c_str(), ios::in))
    is.rdbuf(&inputFb);
  else
    cerr<<"Couldn't open the file!!!!!!\n";

What I would like to know is: is this a good way of doing things?

Thanks,

Mark.



More information about the tuxCPProgramming mailing list