如何获取充当 stdin/stdout 的文件的名称?
我遇到以下问题。我想用 Fortran90 编写一个程序,我希望能够像这样调用:
./program.x < main.in > main.out
除了“main.out”(我可以在调用程序时设置其名称)之外,还必须编写辅助输出,我希望它们具有与“main.in”或“main.out”类似的名称(它们实际上并不称为“main”);但是,当我使用:
INQUIRE(UNIT=5,NAME=sInputName)
sInputName 的内容变为“Stdin”而不是文件名。有没有办法在调用程序时获取链接到 stdin/stdout 的文件名?
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
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)
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??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,I/O 重定向的要点是您的程序不必知道输入/输出文件是什么。在基于 UNIX 的系统上,您无法将命令行参数视为
<; main.in > main.out 实际上是由 shell 处理的,shell 在调用程序之前使用这些文件来设置标准输入和输出。
您必须记住,有时标准输入和输出甚至不是文件,因为它们可能是终端或管道。例如,
因此一种解决方案是重新设计程序,使输出文件成为显式参数。
这样你的程序就知道文件名。代价是您的程序现在负责打开(并且可能创建)文件。
也就是说,在 Linux 系统上,您实际上可以找到标准文件句柄从特殊的 /proc 文件系统指向的位置。每个文件描述符都会有符号链接
抱歉,我不懂 fortran,但检查输出文件的伪代码方式可能是:
记住我之前所说的,没有保证这实际上是一个正常文件。它可以是一个终端设备,一个管道,一个网络套接字,等等......另外,我不知道除了 redhat/centos linux 之外,它还可以在哪些其他操作系统上运行,所以它可能不那么便携。更多的是一种诊断工具。
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.
So one solution is to redesign your program so that the output file is an explicit argument.
That way your program knows the filename. The cost is that your program is now responsible for openning (and maybe creating) the file.
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
Sorry, I don't know fortran, but a psudeo code way of checking the output file could be:
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.
也许内部子例程
get_command
和/或get_command_argument
可以提供帮助。它们是在 fortran 2003 中引入的,或者返回用于调用程序的完整命令行,或者返回指定的参数。Maybe the intrinsic subroutines
get_command
and/orget_command_argument
can be of help. They were introduced in fortran 2003, and either return the full command line which was used to invoke the program, or the specified argument.