在C语言中如何打印文件的文件名被重定向为壳输入 [英] In C how do I print filename of file that is redirected as input in shell

查看:154
本文介绍了在C语言中如何打印文件的文件名被重定向为壳输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ CC交流转换器
。$ / a.out的< inpfilename

我要打印inpfilename在标准输出上。
我怎么做 ?
感谢您提前帮助...


解决方案

您不能准确得到文件名作为输入; shell将处理所有重定向东西瞒着你。

在直接&LT的情况;文件重定向,您可以检索通过使用标准输入相关的文件路径 FSTAT 来得到一个inode编号它,然后同样走在文件层次为查找/ -inum 来获得与其匹配的路径。 (可能有多个这样的文件路径,由于连结)。

但你不应该永远需要做到这一点。正如其他人所说,如果你需要知道文件名,你应该采取的文件名作为参数。<​​/ P>

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

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

解决方案

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.

这篇关于在C语言中如何打印文件的文件名被重定向为壳输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆