Sign in to follow this  
Edzio

C++ unix aka wumpus project - need a file-finder

Recommended Posts

Hello again, I'm stuck on a file loading problem; i need to load an unknown sequential access filename (.txt) and am having trouble with where to assign the variables. this is inside my commandline args : if ((string(argv[1]) == "-f") && (string(argv[2]) == " ")) { ifstream infile; cout << "Sorry, not working yet!" << endl; } else... ? ifstream infile; string filename = ? infile .open(variable?.c_str()) please help

Share this post


Link to post
Share on other sites
If using an invocation like

myapp -f filename.txt

then argv[1] will be "-f" and argv[2] will be "filename.txt". The white space in-between will be already dropped. So your check about argv[2] seems me not correct. I would expect something like

if ( argc >= 3 && string(argv[1]) == "-f" ) {
ifstream infile;
infile.open(argv[2]);
...
}
else ...


to be the way to go. It checks whether argc is at least 3 and hence argv[1] and argv[2] are available. Then it checks whether the "-f" option is given. If true it expects the following argument to be a filename and passes it into the open method.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this