c++ - How to process command line arguments? -
yesterday made simple program in c++ uses arguments passed through command line.
e.g. mydrive:\mypath\myprogram.exe firstword secondword
the program run fine , has to, there's little curiosity have: had write argc --;
before use well, otherwise have run-time crash [the compiler won't speak!].
in particular argc
gives me bad time when don't give word argument program when run it...
now works, isn't bad @ all, wonder why happening! [p.s. making argc --;
, printing it, gives 0
value!]
edit: here istructions use argc
int main(int argc, char *argv[]) { [...] argc --; if(argc > 0){ if(firstarg.find_last_of(".txt") != string::npos){ reading.open(argv[1], ios::binary); [...] } } if ((!(firstarg.find_last_of(".txt") != string::npos)) && argc > 0){ [...] for(int = 1; <= argc; ++){ [...] totranslate = argv[i][j]; [...] totranslate = argv[i][j]; } } }
the arguments include name of program well, argc
@ least 1.
here's typical loop:
int main(int argc, char * argv[]) { (int = 0; != argc; ++i) { std::cout << "argument #" << << ": " << argv[i] << "\n"; } }
alternatively can print backwards:
while (argc--) { std::cout << argv[argc] << "\n"; }
Comments
Post a Comment