steps:
- Let's say I have a C program inputFileName.c
- I run inputFileName with input redirection such as
./inputFileName < file
How can I print the name of the file in my C program that I have typed in the terminal as an input redirection file?
CodePudding user response:
The input redirection is a function of the shell. Your inputFileName
executable see this as standard input. Depending on the exact operating system, you may be able to use system-specific functions to get the information you want, but there is not a standard means of doing so.
CodePudding user response:
Input redirection can be achieved not only with the '<' symbol, but also with '|'.
program < filename
is equivalent to
cat filename | program
From there, one could go to
cat file1 file2 file3 | program
You begin to see why the initial 'stdin' for an executable cannot and does not have a "filename" associated with it.