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

后端 未结 9 1736
情深已故
情深已故 2020-12-10 17:35
$cc a.c
$./a.out < inpfilename

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

9条回答
  •  盖世英雄少女心
    2020-12-10 18:24

    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.

提交回复
热议问题