将未格式化(二进制数据)写入标准输出 [英] Write unformatted (binary data) to stdout

查看:185
本文介绍了将未格式化(二进制数据)写入标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Fortran 90程序中将未格式化(二进制)数据写入STDOUT。我正在使用AIX Unix,不幸的是,它不会让我将单元6打开为未格式化。我想我会尝试打开 / dev / stdout ,而不是使用不同的单元号,但是 / dev / stdout 会不存在于AIX中(尽管这种方法在Linux下工作)。

基本上,我想将我的程序直接输出到另一个程序中,这样就避免了一个中间文件,一个有点像 gzip -c 。考虑到上面遇到的两个问题,是否还有其他一些方法可以实现这一目标?解决方案

我会尝试将数据由 TRANSFER()转换为一个长字符,并用非递归I / O打印。问题将是您的处理器对记录长度的限制。如果太短,最终会在某处出现意外的记录结束。你的处理器也许不会按照你喜欢的方式写出不可打印的字符。



即类似于

<$ p $ (*,'(a)')(b,b
$ b缓冲区=传输(数据,缓冲区)

,advance ='no')trim(缓冲区)

我在不可打印字符中看到的最大问题。另请参阅带有非前进I / O的惊喜

---编辑---
另一种可能性是尝试使用文件 / proc / self / fd / 1 / dev / fd / 1



测试:

  open(11,file ='/ proc / self / fd / 1',access ='stream',action ='write') 
write(11)11
write(11)1.1
close(11)
end


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?

解决方案

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

这篇关于将未格式化(二进制数据)写入标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆