In C how do I print filename of file that is redirected as input in shell

$cc a.c
$./a.out < inpfilename

I want to print inpfilename on stdout. How do I do that ? Thanks for the help in advance...

By : Nullpoet


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[] ) { ... }


$ ./a.out 
By : Chen Levy

I don't think it's possible, since

By : mlibby

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.

By : ZJR

This video can help you solving your question :)
By: admin