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

Jack Lloyd lloyd at acm.jhu.edu
Wed Nov 14 03:42:05 UTC 2001


I usually use an istream&, but that's basically the way I've always done
it. Most Unix programs do the same thing, either with FILE*s or file
descriptors. -J

On Tue, 13 Nov 2001, Mark Phillips wrote:

> 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.
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
>




More information about the tuxCPProgramming mailing list