Write unformatted (binary data) to stdout

后端 未结 2 1005
北海茫月
北海茫月 2021-01-19 21:17

I want to write unformatted (binary) data to STDOUT in a Fortran 90 program. I am using AIX Unix and unfortunately it won\'t let me open unit 6 as \"unformatted\". I thought

相关标签:
2条回答
  • 2021-01-19 21:34

    I would try to convert the data by TRANSFER() to a long character and print it with nonadvancing i/o. The problem will be your processors' limit for the record length. If it is too short you will end up having an unexpected end of record sign somewhere. Also your processor may not write the unprintable characters the way you would like.

    i.e., something like

    character(len=max_length) :: buffer
    
    buffer = transfer(data,buffer)
    
    write(*,'(a)',advance='no') trim(buffer)
    

    The largest problem I see in the unprintable characters. See also A suprise with non-advancing I/O

    ---EDIT--- Another possibility, try to use file /proc/self/fd/1 or /dev/fd/1

    test:

    open(11,file='/proc/self/fd/1',access='stream',action='write')
    write(11) 11
    write(11) 1.1
    close(11)
    end
    
    0 讨论(0)
  • 2021-01-19 21:35

    This is more of a comment/addition to @VladimirF than a new answer, but I can't add those yet. You can first inquire about the location of the preconnected I/O units and then open the unformatted connection:

    character(1024) :: stdout
    inquire(6, name = stdout)
    open(11, file = stdout, access = 'stream', action = 'write')
    

    This is probably the most convenient way, but it uses stream access, a Fortran 2003 feature. Without this, you can only use sequential access (which adds header data to each record) or direct access (which does not add headers but requires a fixed record length).

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