Input: File or stdin?
I've written a program in C++ that takes an input filename as a command line parameter, and that filename gets passed to the processing function as a char*, then the processing function declares an ifstream and does it's work.
What I would like to do, is if no filename is given at the command line as an argument, then stdin is used for input. The problem is, what can I pass to the processing function as a parameter to tell it to use stdin? I assume I'd have to change the parameter type, but to what? I doubt I need to make another version of that function...
Anyone wanna give me a hint? Does this rambling even make sense?
i don't know what stdin is... so... anyways.. why not just create two functions with the same name and use an IF statement and depending on the IF statement one of the functions is called... or pass both the filename & stdin to the same function and do an IF statement there... or lastly... (i think this last one would work and be the best) use a TEMPLATE so the program decides what type to pass into the function at runtime instead of declaring it in the code before hand..
Derive an instance of an istream that either points to an ifstream or cin depending on whether a command line argument has been passed. Something like -
is = new ifstream(cl);
is = &cin;
Don't forget to delete if it's a fstream.
Thank you JoeSixpack. With a little messing around with the stuff I had already coded, that worked perfectly.