How would you ask for the user to input files as arguments to be used (as many as they would like)? Also How would you print to a file?
scanf("%s", user_filename);
FILE *fp;
fp = fopen (user_filename, "r");
I have tried doing various things to it but I can only get it to take one file.
The easiest way to pass some file names to your C program is to pass them as arguments to your C program.
Arguments are passed to a C program using the parameters to
main
:The value
argc
indicates how many parameters there are, andargv[]
is an array of string pointers containing the arguments. Note that the first argument at index0
(the string pointed to byargv[0]
) is the command name itself. The rest of the arguments passed to the command are inargv[1]
,argv[2]
, and so on.If you compile your program and call it like this:
Then the value of
argc
will be4
(remember it includes the command) and theargv
values will be:In your program then, you only need to check
argc
for how many parameters there are. Ifargv > 1
, then you have at least one parameter starting atargv[1]
:This is just one of several different ways to do it (functionally and stylistically), depending upon how you need to process the files.