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

前端 未结 2 1107
后悔当初
后悔当初 2021-01-19 13:31

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
<         


        
相关标签:
2条回答
  • 2021-01-19 13:42

    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.

    0 讨论(0)
  • 2021-01-19 13:44

    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.

    0 讨论(0)
提交回复
热议问题