Reputation: 3605
I had a terrible time with file input from command line arguments last semester and I need to utilize it for an exercise that I am working on. I have coded a simple shell just to get it working:
prob_5.c
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
FILE *fp;
int c;
for (i = 1; i < argc; i++) {
fp = fopen(argv[i], "r");
if (fp == NULL) {
fprint(stderr, "cat: can't open %s\n", argv[i]);
continue;
}
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
}
return 0;
}
I can't seem to remember what the commands are for invoking my program from the command line. I have tried:
gcc -o prob_5 -g -ansi prob_5.c
I have reformatted my computer since last semester, so perhaps I am missing a System Path?
Upvotes: 0
Views: 13979
Reputation: 4557
It looks like your program just expects one argument: the file name. You also have to compile it first.
$ gcc -o prob_5 prob_5.c
$ ./prob_5 input_file.txt
If it is not compiling, then you have another issue. What is returned when you execute gcc
?
Upvotes: 3