将数据写入列(Fortran) [英] Write data to file in columns (Fortran)
问题描述
WRITE(*,*)input
来将值分组到列? WRITE
总是在每次调用后放置一个新行
,这就是问题所在。 代码示例:
open(unit = 4,file ='generated_trajectories1.dat',form ='formatted ')
do time_nr = 0,N
write(4,*)dble(time_nr)* dt,initial_traj(time_nr)
end do
$ b现在的重点是把它写在不同的列中。
解决方案您可以使用隐含的DO循环将值作为单个记录写入。比较下面的两个例子:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b write(*,'(2I4)')i,2 * i
end do
它产生:
1 2
2 4
3 6
...
使用隐含的DO循环,它可以被重写为:
integer :: i
write(*,'(10(2I4))')(i,2 * i,i = 1,10)
这个产生:
pre > 1 2 2 4 3 6 ...
在编译时不固定,你可以使用< n>
扩展名(不支持 gfortran
): (*,'(n)(2I4))')(i,2 * i,i = 1 ,n)
需要重复的次数(2I4)
从变量 n
的值编辑描述符。在GNU Fortran中,您可以使用内部文件创建适当的编辑描述符:
字符(len = 20):: myfmt
write(myfmt,'((,I0,(2I4)))')n
write(*,fmt = myfmt)(i,2 * i,i = 1 ,n)
当然,它也适用于列表定向输出(格式为< $($,$)$($,$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' i,i = 1,10)
I need to write some data to file in Fortran 90. How should I use WRITE (*,*) input
to have the values grouped in columns? WRITE
always puts a new line
after each call, that's the problem.
code example:
open (unit = 4, file = 'generated_trajectories1.dat', form='formatted')
do time_nr=0, N
write (4,*) dble(time_nr)*dt, initial_traj(time_nr)
end do
And now the point is to have it written in separate columns.
You can use implied DO loops to write values as single records. Compare the following two examples:
integer :: i
do i=1,10
write(*,'(2I4)') i, 2*i
end do
It produces:
1 2
2 4
3 6
...
Using implied DO loops it can rewritten as:
integer :: i
write(*, '(10(2I4))') (i, 2*i, i=1,10)
This one produces:
1 2 2 4 3 6 ...
If the number of elements is not fixed at compile time, you can either use the <n>
extension (not supported by gfortran
):
write(*, '(<n>(2I4))') (i, 2*i, i=1,n)
It takes the number of repetitions of the (2I4)
edit descriptor from the value of the variable n
. In GNU Fortran you can first create the appropriate edit descriptor using internal files:
character(len=20) :: myfmt
write(myfmt, '("(",I0,"(2I4))")') n
write(*, fmt=myfmt) (i, 2*i, i=1,n)
Of course, it also works with list directed output (that is output with format of *
):
write(*, *) (i, 2*i, i=1,10)
这篇关于将数据写入列(Fortran)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!