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

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

问题描述

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

  ./ program.x< main.in> main.out除了main.out(我在调用程序时可以设置的名字)之外,还有一个名为main.out的主键。

,次要输出必须写出来,我希望它们有一个与main.in或main.out类似的名称(它们实际上并不称为main);然而,当我使用:

  INQUIRE(UNIT = 5,NAME = sInputName)

sInputName的内容变为Stdin而不是文件的名称。有什么方法可以获得当程序被调用时链接到stdin / stdout的文件的名字吗?

解析方案

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



要记住有时标准输入和输出不会是文件,因为它们可能是终端或管道。例如

  ./ generate_input | ./program.x |减少

因此,一种解决方案是重新设计程序,以便输出文件是明确的参数。 p>

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

这样你的程序就知道文件名了。成本是,你的程序现在负责打开(也许创建)文件。



也就是说,在Linux系统上,你可以真正发现你的标准文件处理的地方指向特殊/ proc文件系统。每个文件描述符都会有符号链接。
$ b $ pre $ / proc /< process_id> / fd / 0 - > standard_input
/ proc /< process_id> / fd / 1 - > standard_output
/ proc /< process_id> / fd / 2 - > standard_error

对不起,我不知道fortran,但检查输出文件的psudeo代码方式可能be:

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

请记住我所说的早些时候,没有这样做,这实际上是一个正常的文件。它可能是一个终端设备,一个管道,一个网络套接字,无论如何...另外,我不知道除了redhat / centos linux以外还有哪些操作系统可以工作,所以它可能不是那么便携。更多的诊断工具。

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??

解决方案

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.

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

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 ) )
    ...

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天全站免登陆