You know the common stdio idiom that stdin is specified by
a filename of “-“, e.g.
if ((strcmp(fname, "-"))
fp = fopen(fname);
else
fp = stdin;
What’s the best way to do this with an ifstream instance? I’ve received
a bit of code that has an ifstream as part of a class and I’d
like to add code to do the equivalent, something like:
if ( filename == "-")
logstream = cin; // **how do I do this*?*
else
logstream.open( filename.c_str() );
cinis not anifstream, but if you can useistreaminstead, then you’re in to win. Otherwise, if you’re prepared to be non-portable, just open/dev/stdinor/dev/fd/0or whatever. 🙂If you do want to be portable, and can make your program use
istream, here’s one way to do it:The
noopis to specify a deleter that does nothing in thecincase, because, well,cinis not meant to be deleted.