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

后端 未结 9 1721
情深已故
情深已故 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:21

    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.

提交回复
热议问题