如何获取充当标准输入/标准输出的文件的名称? [英] How to get the name of a file acting as stdin/stdout?

查看:21
本文介绍了如何获取充当标准输入/标准输出的文件的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题.我想在 Fortran90 中编写一个程序,我希望能够像这样调用:

I'm having the following problem. I want to write a program in Fortran90 which I want to be able to call like this:

./program.x < main.in > main.out

除了main.out"(我可以在调用程序时设置其名称)之外,必须编写辅助输出,我希望它们具有与main.in"或main.out"相似的名称(它们实际上并不被称为主要");但是,当我使用:

Additionally to "main.out" (whose name I can set when calling the program), secondary outputs have to be written and I wanted them to have a similar name to either "main.in" or "main.out" (they are not actually called "main"); however, when I use:

INQUIRE(UNIT=5,NAME=sInputName)

sInputName 的内容变成了Stdin"而不是文件名.有什么办法可以在调用程序时获取链接到stdin/stdout的文件名吗??

The content of sInputName becomes "Stdin" instead of the name of the file. Is there some way to obtain the name of files that are linked to stdin/stdout when the program is called??

推荐答案

不幸的是,I/O 重定向的要点在于您的程序不必知道输入/输出文件是什么.在基于 unix 的系统上,您不能将命令行参数视为 <main.in >main.out 实际上是由 shell 处理的,shell 在调用程序之前使用这些文件设置标准输入和输出.

Unfortunately the point of i/o redirection is that you're program doesn't have to know what the input/output files are. On unix based systems you cannot look at the command line arguments as the < main.in > main.out are actually processed by the shell which uses these files to set up standard input and output before your program is invoked.

您必须记住,有时标准输入和输出甚至不是文件,因为它们可能是终端或管道.例如

You have to remember that sometimes the standard input and output will not even be files, as they could be a terminal or a pipe. e.g.

./generate_input | ./program.x | less

因此,一种解决方案是重新设计您的程序,使输出文件成为显式参数.

So one solution is to redesign your program so that the output file is an explicit argument.

./program.x --out=main.out

这样你的程序就知道文件名.代价是您的程序现在负责打开(甚至创建)文件.

That way your program knows the filename. The cost is that your program is now responsible for openning (and maybe creating) the file.

也就是说,在 Linux 系统上,您实际上可以从特殊的/proc 文件系统中找到标准文件句柄指向的位置.每个文件描述符都有符号链接

That said, on linux systems you can actually find yout where your standard file handles are pointing from the special /proc filesystem. There will be symbolic links in place for each file descriptor

/proc/<process_id>/fd/0 -> standard_input
/proc/<process_id>/fd/1 -> standard_output
/proc/<process_id>/fd/2 -> standard_error

抱歉,我不知道 fortran,但检查输出文件的伪代码方式可能是:

Sorry, I don't know fortran, but a psudeo code way of checking the output file could be:

out_name = realLink( "/proc/"+getpid()+"/fd/1" )
if( isNormalFile( out_name ) )
    ...

请记住我之前所说的,没有保证这实际上是一个普通文件.它可能是一个终端设备、一个管道、一个网络套接字等等……另外,我不知道除了 redhat/centos linux 之外还有哪些其他操作系统可以工作,所以它可能不是那么便携.更多的是诊断工具.

Keep in mind what I said earlier, there is no garauntee this will actually be a normal file. It could be a terminal device, a pipe, a network socket, whatever... Also, I do not know what other operating systems this works on other than redhat/centos linux, so it may not be that portable. More a diagnostic tool.

这篇关于如何获取充当标准输入/标准输出的文件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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