At the Unix command line, I need to use input redirection to specify the input file:
./a.out < filename
Here is my source code:
int main(int argc, char *argv[]) {
char fileName[30];
fgets(fileName, sizeof fileName, stdin);
printf("%s\n", fileName);
}
The program prints an integer instead of the filename. I get the same results if I use scanf("%s", fileName);
My google-fu has failed me. What am I doing wrong?
Edit: It is required to use input redirection (<
, specifically), so command line arguments are not an option. However, I do not need to actually know the file name, I just need to read newline-delimited integers from the file. It appears that I am asking the wrong question.