tags:

views:

178

answers:

2
// stream from file.
ifstream file;

int main (int argc, char * argv[]) {

// get argument passed from command line
// This is file name
if (argc != 2 ) {
 cout << "use:  ./executable <filename>";

}else {
 //cout << "You are using filename: " << argv[1];

 // start the file stream
 file (argv[1]);
}

Is there any reason why file(argv[1]) would be giving an error? Can I have a ifstream as a global variable?

+5  A: 

You're trying to call the ifstream's () operator (which doesn't exist), when you should be using file.open(argv[1]).

Besides that, there's nothing illegal about having a global ifstream.

Mark Rushakoff
That makes sense. I didn't realize that the I was using a constructor. Thank you for the quick response.
Jeffrey Guenther
Actually, he's calling `ifstream::operator()` which doesn't exist.
rlbond
I stand corrected. Thanks for pointing that out.
Mark Rushakoff
+2  A: 

You can have the ifstream as a global variable (whether this is good style is a different question).

The problem seems to be that you're attempting to use the constructor: file(argv[1])

The global variable would already be constructed (using the default constructor) at this point, and you will instead need to use the open method.

file.open( argv[1] );
Alex Deem
Yes, I realize the use of a global variable is not necessary the best idea. For this project, it will be sufficient. Thank you for the additional explanation.
Jeffrey Guenther