-
Loading multiple files
I need to be able to prompt the user to enter files that they want linked in a program, (i.e enter the files to be linked: file1.c file2.c file3.c) all on one line and separated by a space, I know how to load one file, but not multiple files, can anybody help?
-
take the file names at the command prompt.
argv[0] is the name of the file that u r executing,
argv[1] is the name of the first file that u will bw working on,
argv[2] is the name of the second file that u will bw working on and so on...
the total number of arguments that u have entered at the command prompt is know, i.e., the it is stored in argc..
now,
Code:
int i;
FILE *fp;
for(i=1;i<=argc;i++)
{
fp=fopen(argv[i],"r");
/ *
whatever u want to do with hte file
*/
fclose(fp);
}
I suppose this is what u were looking for.
-
loading multiple files
looking at what you wrote, it seems to be what i'm looking for, and i thank you for that, but, just to be sure, argv[0] is the file i'm executing, [1] is the file i'll be working on. also will the file names automatically be assigned to the arguments, because, the user needs to enter all the file names at once, it's not enter a file name one at a time, it's all on one command line, on a unix system
-
> argv[0] is the file i'm executing,
yes, u print its contents, u get " ./a.out "
>will the file names automatically be assigned to the arguments, because, the user needs to enter all the file names at once, it's not enter a file name one at a time, it's all on one command line, on a unix system
yes, u have enter all the names at once, argv is the array of char pointers, so each element in that array points to the strings that u have entered at the prompt. U dont have to worry about the mapping part.
-
> for(i=1;i<=argc;i++)
Should be < argc