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

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

问题描述

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

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

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

推荐答案

您无法获得与输入完全相同的文件名;shell 会在不告诉你的情况下处理所有重定向的事情.

You can't get the filename exactly as input; the shell will handle all that redirection stuff without telling you.

在直接<的情况下file 重定向,您可以通过使用 fstat 来检索与 stdin 关联的文件路径以获取它的 inode 编号,然后类似于 find/-inum 遍历文件层次结构获得匹配的路径.(由于链接的原因,这样的文件路径可能不止一个.)

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 中,如何打印在 shell 中作为输入重定向的文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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