tags:

views:

106

answers:

4

Is there a common interface for cin and file input?

I want to make a program that has an optional parameter

prog [input-file]

If an input file is specified, then it should read from the file, and if not, it should read from cin.

From what I can tell, they both implement istream. How would you set up it so that I could do something like in >> var, where in is an istream.

+7  A: 

You can write a function that takes a reference to an std::istream:

void do_input(std::istream& the_istream)
{
    int my_awesome_variable;
    the_istream >> my_awesome_variable;
}

Usage example:

int main()
{
    // reading from stdin:
    do_input(std::cin);

    // reading from a file:
    std::ifstream fs("test.txt");
    do_input(fs);
}
James McNellis
A: 
istream& input = cin;

or

inputFileStream ifstream(fileName);
istream& input = inputFileStream;

However I think that this is not a very good method since cin doesn't have a close() whereas ifstream does.

Novikov
You cannot retarget a reference; even if you fixed the glaring syntactic errors, `input = inputFileStream` cannot work. Concerning `close()`, usually you don't have to call `close()` anyway; you just let the destructor handle it.
James McNellis
Meant for those two to be two separate code blocks.
Novikov
+5  A: 
#include <iostream>
#include <fstream>

int main(int argc, char **argv)
{
    std::ifstream f;
    if (argc >= 2) {
        f.open(argv[1]);
    }
    std::istream &in = (argc >= 2) ? f : std::cin;

    // use in here
}

You could shift some of this work into a helper class to make it clearer what's going on (note that this has slightly different behavior in the case where the file can't be opened):

#include <iostream>
#include <fstream>

class ifstream_or_cin_t {
    std::ifstream f;

public:
    ifstream_or_cin_t(const char *filename)
    {
        if (filename) {
            f.open(filename);
        }
    }

    operator std::istream &() { return f.is_open() ? f : std::cin; }
};

static void do_input(std::istream &in)
{
    // use in...
}

int main(int argc, char **argv)
{
    do_input(
        ifstream_or_cin_t((argc >= 2) ? argv[1] : NULL));
}
dave
Snap. :-) I did the same.
Martin York
sbi
@sbi How's that for decomposition? ;)
dave
@dave: For one, I'm not a fan of introducing classes for algorithms. Algorithms are best implemented as functions, and classes are good suited to store objects, that is: a state (member data) plus operations (member functions). Picking a stream is an algorithm. But that might just be my preferences. The second problem is less disputable: if you can't open the file, this code will silently read from the console. Imagine a user mistyping a filename and then sitting and staring at the blinking prompt, not knowing what to make of it!
sbi
@dave, @sbi, @James: After thinking about it I do prefer James version for the simpler types of program. But I would need to think some more for situations where there were a large number of command line arguments that needed to be parsed (which you generally don't do in main).
Martin York
+1  A: 

My 2P worth:

#include <iostream>
#include <fstream>
#include <string>

extern char const* findFlag(int,char*[],char const*);
int main(int argc,char* argv[])
{
    std::string     isFile  = findFlag(argc,argv,"-f");
    std::ifstream   file;

    if (isFile != "")
    {   file.open(isFile.c_str());
    }
    std::istream&   data    = isFile != "" ? file : std::cin;


}
Martin York
Just a minor stylistic point... Because with ordinary c-strings a == or != comparison will not work, I prefer to explicitly create an std::string from a c-string when I am comparing them.
San Jacinto
@San Jacinto: I understand your point in the general case. But I think x == "" is actually clearer than x == std::string("").
Martin York
Wouldn't that better be `!isFile.empty()`?
sbi