How to get the name of a file acting as stdin/stdout?

浪子不回头ぞ 提交于 2019-12-20 02:47:26

问题


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


回答1:


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.




回答2:


Maybe the intrinsic subroutines get_command and/or get_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.



来源:https://stackoverflow.com/questions/5602663/how-to-get-the-name-of-a-file-acting-as-stdin-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!