Search code examples
iofortran

Write unformatted (binary data) to stdout


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 I would try and open /dev/stdout instead under a different unit number, but /dev/stdout does not exist in AIX (although this method worked under Linux).

Basically, I want to pipe my programs output directly into another program, thus avoiding having an intermediate file, a bit like gzip -c does. Is there some other way I can achieve this, considering the two problems I have encountered above?


Solution

  • 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