I am having issue reading from standard input. In other words I want to run my program like: ./WordCount < Sample.txt
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * file;
char filename[1000], c;
printf("Enter filename to read\n");
scanf("%s", filename);
file = fopen (filename, "r");
if (file == NULL) {
printf("Invalid File\n");
exit(0);
}
c = fgetc(file);
while (c != EOF){
printf("%c", c);
c = fgetc(file);
}
fclose(file);
return(0);
}
I have been looking for hours and tried many options. Maybe I was not looking in the right place. I am not sure what I need to change in my code in order to read from standard input. I know that scanf reads from stdin but I want to use the redirection from a file command when running the program.