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

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -