输出格式:gfortran中有太多的空格 [英] Output formatting: too much whitespace in gfortran

查看:187
本文介绍了输出格式:gfortran中有太多的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gfortran 4.6。这个代码:

pre $ $ $ $ $ $

$ INTEGER :: i = 1, j = 3

WRITE(*,*)整数i是,i,,j是,j。
END PROGRAM f1

产生这个控制台输出,它有太多的空格: p>

 整数i是1,j是3。 

是否有一些设置可以设置,以便在第一个标记之前没有空格(integer ),所以令牌之间的空白只是一个空格?我知道一个修正是

$ p $ WRITE(*,'(A,I1,A,I1,A)')integer i是,我,而j是,j。

但是每次我有一个打印语句时,这似乎非常麻烦 - 宁愿它是更像是C ++,你可以在输出中明确地写出任何空格。

解决方案 c>写(*,*)是为了方便。没有设置来改变它的行为。不同的编译器会产生不同的输出。相反,您可以使用格式化的IO。在这种情况下,您可以使用 I0 作为格式,这将产生所需的数字位数,而 I1 输出单个数字的整数。这是可以的,如果这些是最大的值将被输出。

$ $ p $ $ $ $ c $写入(*,'(整数我是, I0,和j是,I0,。)')i,j


Using gfortran 4.6. This code:

PROGRAM f1
IMPLICIT NONE

INTEGER :: i=1, j=3

WRITE(*,*) "integer i is ", i, ", and j is ", j, "."
END PROGRAM f1

produces this console output, which has way too much whitespace:

 integer i is            1 , and j is            3 .

Is there some setting I can set so that there is no space before the first token ("integer"), and so the whitespace between tokens is just one space? I know one fix is

WRITE(*,'(A,I1,A,I1,A)') "integer i is ", i, ", and j is ", j, "."

but this seems very cumbersome to have to do every time I have a print statement - would rather it be somewhat more like C++ where you explicitly write any whitespace in the output.

解决方案

List-directed IO, i.e, write (*, *) is meant as a convenience. There are no settings to change its behavior. Different compilers will produce different output. Instead you can, as you have identified, use formatted IO. In this case you can use I0 as the format, which will produce the required number of digits, while I1 will only output single-digit integers. Which is OK if those are the largest values that will be output.

WRITE(*,  '( "integer i is ", I0, ", and j is ", I0, "." )' )  i, j

这篇关于输出格式:gfortran中有太多的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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