finiteloop
finiteloop

Reputation: 4496

declaring generic istream in c++

I need to write a program that reads in either from ifstream or cin, depending on parameters passed into the program at runtime.

I was planning on doing the following:

 istream in;

 if(argv[1] == "cin")
 {
      in = cin;
 }
 else
 {
      ifStream inFile;
      inFile.open(argv[1].c_str());
      in = inFile;
 }

However, istream in protected, and I can't declare istream in. Is there a way to declare such a generic in stream?

Upvotes: 3

Views: 7680

Answers (3)

eatorres
eatorres

Reputation: 179

You can also do this without the pointers:

ifStream inFile;
istream in( argv[1] == "cin" ? cin : inFile.open(argv[1]));

Upvotes: 0

Jay Michaud
Jay Michaud

Reputation: 405

An alternative design is to write your code using cin, and then use input redirection when running the program if you want to accept input from a file. This doesn't answer your exact question, but it is a simpler design for the case you presented.

For example, instead of

program.exe cin

you would just run

program.exe

and instead of

program.exe myfile.txt

you would run

program.exe < myfile.txt

This works on *nix and Windows.

Upvotes: 1

Diego Sevilla
Diego Sevilla

Reputation: 29021

Try with an istream* instead. Note, however, that you have to change your code slightly. Using pointers you have to preserve the memory area of the object that you're pointing. In other words, the "inFile" variable cannot be declared there, as it won't exist out of the else. The code could be, then:

 istream* in;
 ifStream inFile;

 if(!strcmp(argv[1],"cin"))
 {
      in = &cin;
 }
 else
 {
      inFile.open(argv[1]);
      in = &inFile;
 }
 // use *in

(Note also the modifications in the string handling. I modified them as an example.)

Upvotes: 3

Related Questions