$cc a.c
$./a.out < inpfilename
I want to print inpfilename on stdout. How do I do that ? Thanks for the help in advance...
$cc a.c
$./a.out < inpfilename
I want to print inpfilename on stdout. How do I do that ? Thanks for the help in advance...
Why do you want to do this? All your program a.out
is passed from the shell, is an open file descriptor, stdin.
The user might as well do this:
cat inpfilename | ./a.out
and now you have absolutely no filename to use (except /dev/stdin).
If a.out
needs to work with filenames, why not take the file as a command-line argument?
Only the parent shell is going to know that. The program, a.out is always going to see it as stdin.
Your operating system will supply your program with input from this file. This is transparent to your program, and as such you don't get to see the name of the file. In fact, under some circumstances you will be fed input which doesn't come from a file, such as this:
ls | ./a.out
What you're after is very system-specific. Probably a better solution is to pass the filename as a parameter. That way you get the filename, and you can open it to read the content.
As you put it the process that runs a.out has no notion of the file name of the file that provides its' standard input.
The invocation should be:
$ ./a.out inputfilename
and parse argv
in int main( int argc, char* argv[] ) { ... }
or
$ ./a.out <<< "inputfilename"
And get the filename from stdin.
Then in a.c you need to fopen
that file to read it's content.
You can't get the filename exactly as input; the shell will handle all that redirection stuff without telling you.
In the case of a direct < file
redirection, you can retrieve a filepath associated with stdin by using fstat
to get an inode number for it then walking the file hierarchy similarly to find / -inum
to get a path that matches it. (There might be more than one such filepath due to links.)
But you shouldn't ever need to do this. As others have said, if you need to know filenames you should be taking filenames as arguments.
I don't think it's possible, since <
just reads the contents of inpfilename
to STDIN.
If you want inpfilename to be available to your program, but you also want to be able to accept data from STDIN, set up your program to accept a filename argument and fopen that to a FILE. If no argument is given assign STDIN to your FILE. Then your input reading routine uses functions like fscanf rather than scanf, and the FILE that you pass in is either a link to the fopened file or STDIN.
An fstat(0,sb)
(0 is stdin file descriptor) will give you details on the input file, size, permissions (called mode) and inode of the device it resides on.
Anyway you won't be able to tell its path: as unix inodes have no idea what path they belong to, and technically (see ln
) they could belong to more than one path.
G'day,
As pointed out in the above answer all you see is the open file descriptor stdin.
If you really want to do this, you could specify that the first line of the input file must be the name of the file itself.
HTH