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

后端 未结 9 1716
情深已故
情深已故 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条回答
  •  -上瘾入骨i
    2020-12-10 18:32

    In fact, it is possible to get filename from procfs, since /proc/*/fd contains symlink to opened files:

    char filename[bufsize];
    int sz = readlink("/proc/self/fd/0", filename, bufsize-1);
    filename[sz] = 0;
    puts(filename);
    

提交回复
热议问题