Issue
I'm a bit confused as to how I could add certain parameters in the Ubuntu terminal when using the GNU C compiler to compile the program. For example:
gcc -o question question.c
./question -e -f someFile.txt
where -f would open this specific file 'someFile.txt' (any file) and -e would let me access a specific function inside my code.
I tried this with void main(int argc, char* argv[]) but with that I would have to specify the number of arguments I would have to pass i.e. ./question 3 -e -f resources.txt, which I would not like to do.
Is there any other way I could attempt this?
Thank you in advance!!!
Solution
#include <stdio.h>
int main(int argc, char **argv) {
printf("program was supplied %d arguments.\n", argc - 1);
for (int k = 0; k < argc; k++) printf("argv[%d] is %s\n", k, argv[k]);
if (!strcmp(argv[1], "-e")) printf("The first argument provided is -e\n");
}
For advanced usage you may want to read about getopt
Answered By - pmg Answer Checked By - Willingham (WPSolving Volunteer)