擅长:python、mysql、java
<p>不幸的是,i/o重定向的要点是,您的程序不必知道输入/输出文件是什么。在基于unix的系统上,您不能查看命令行参数,因为<code>< main.in > main.out</code>实际上是由shell处理的,在调用程序之前,shell使用这些文件设置标准输入和输出</p>
<p>您必须记住,有时标准输入和输出甚至不是文件,因为它们可能是终端或管道。e、 g</p>
<pre><code>./generate_input | ./program.x | less
</code></pre>
<p>因此,一种解决方案是重新设计程序,使输出文件成为显式参数</p>
<pre><code>./program.x out=main.out
</code></pre>
<p>这样你的程序就知道文件名了。成本是您的程序现在负责打开(或者创建)文件</p>
<p>也就是说,在linux系统上,您实际上可以从特殊的/proc文件系统找到标准文件句柄指向的位置。每个文件描述符都有符号链接</p>
<pre><code>/proc/<process_id>/fd/0 -> standard_input
/proc/<process_id>/fd/1 -> standard_output
/proc/<process_id>/fd/2 -> standard_error
</code></pre>
<p>抱歉,我不知道fortran,但检查输出文件的psudeo代码方法可以是:</p>
<pre><code>out_name = realLink( "/proc/"+getpid()+"/fd/1" )
if( isNormalFile( out_name ) )
...
</code></pre>
<p>记住我之前说过的,没有garauntee,这实际上是一个普通文件。它可能是一个终端设备,一个管道,一个网络插座,无论什么。。。另外,我不知道除了redhat/centos linux之外,它还能在其他什么操作系统上工作,所以它可能没有那么便携。更多的是诊断工具</p>