Question:
I have a small console program that requires I pass arguments for it to run specific functions. I want to change the code so it automatically runs a set of command switches but cannot figure out how to do so. If I do
argv[0] = "-a -b -c"
I get all kinds of errors.
Answer:
I don’t know what your code is doing but argv[0] should produce a valid string. The code snippet below iterates through the list of command line arguments. Perhaps you can use this as a starting point to get the information you need. Note that argv[0], the first argument, is always the name of the executable file.
#includeint main(int argc, char* argv[]){ int i; for (i = 0; i < argc; i++) { printf("Arg %d = %s ", i, argv[i]); } return 0;}