程序无法写入 FORTRAN 中的文件
Program failing to write to a file in FORTRAN
我一直在研究 FORTRAN 并决定编写一个简单的 Blackjack 程序,将 win/loss 统计数据保存在一个文件中。问题是,每次我执行程序时,它都会写入一个新数组,而不是覆盖文件中现有的数组。
相关代码:
open(15, file='placar.dat') !opens the file containing a (0,0,0,0) array
integer, dimension(1:4)::round=0' !This is the array which contains the stats for a
subroutine r(v) !given round, e.g.: player 1 wins and player
integer, intent(in),dimension(1:4)::v !2 loses, round(1)=1,
integer, dimension(1:4)::vx !round(2)=0, round(3)=0, round(4)=1
read(15,*)vx !This line reads the array contained in the file to
!the array vx.
do i=1,4,1
vx(i)=vx(i)+v(i) !this will add the round statistics passed to this
end do !subroutine to the vx array.
write(15,*)(vx(i),i=1,4) !this line should overwrite the old array contained
end subroutine r !in the file with the new values. But this does not
!happen and a new line is written.
我认为您的错误是因为读取原始数组会使文件游标前进。因此,当您写回文件时,您的光标已经超过了数组。
我相信倒带(将光标设置到初始点)文件会纠正此问题:
open(15, file='placar.dat') !opens the file containing a (0,0,0,0) array
integer, dimension(1:4)::round=0' !This is the array which contains the stats for a
subroutine r(v) !given round, e.g.: player 1 wins and player
integer, intent(in),dimension(1:4)::v !2 loses, round(1)=1,
integer, dimension(1:4)::vx !round(2)=0, round(3)=0, round(4)=1
read(15,*)vx !This line reads the array contained in the file to
!the array vx.
do i=1,4,1
vx(i)=vx(i)+v(i) !this will add the round statistics passed to this
end do !subroutine to the vx array.
rewind(15) !rewind file
write(15,*)(vx(i),i=1,4) !this line should overwrite the old array contained
end subroutine r !in the file with the new values. But this does not
!happen and a new line is written.
但是请注意,这会覆盖从文件开头开始的内容,因此如果您在该文件中不仅有数组,它还会覆盖其他内容,可能会以某种方式使输出看起来像很奇怪。
我一直在研究 FORTRAN 并决定编写一个简单的 Blackjack 程序,将 win/loss 统计数据保存在一个文件中。问题是,每次我执行程序时,它都会写入一个新数组,而不是覆盖文件中现有的数组。
相关代码:
open(15, file='placar.dat') !opens the file containing a (0,0,0,0) array
integer, dimension(1:4)::round=0' !This is the array which contains the stats for a
subroutine r(v) !given round, e.g.: player 1 wins and player
integer, intent(in),dimension(1:4)::v !2 loses, round(1)=1,
integer, dimension(1:4)::vx !round(2)=0, round(3)=0, round(4)=1
read(15,*)vx !This line reads the array contained in the file to
!the array vx.
do i=1,4,1
vx(i)=vx(i)+v(i) !this will add the round statistics passed to this
end do !subroutine to the vx array.
write(15,*)(vx(i),i=1,4) !this line should overwrite the old array contained
end subroutine r !in the file with the new values. But this does not
!happen and a new line is written.
我认为您的错误是因为读取原始数组会使文件游标前进。因此,当您写回文件时,您的光标已经超过了数组。
我相信倒带(将光标设置到初始点)文件会纠正此问题:
open(15, file='placar.dat') !opens the file containing a (0,0,0,0) array
integer, dimension(1:4)::round=0' !This is the array which contains the stats for a
subroutine r(v) !given round, e.g.: player 1 wins and player
integer, intent(in),dimension(1:4)::v !2 loses, round(1)=1,
integer, dimension(1:4)::vx !round(2)=0, round(3)=0, round(4)=1
read(15,*)vx !This line reads the array contained in the file to
!the array vx.
do i=1,4,1
vx(i)=vx(i)+v(i) !this will add the round statistics passed to this
end do !subroutine to the vx array.
rewind(15) !rewind file
write(15,*)(vx(i),i=1,4) !this line should overwrite the old array contained
end subroutine r !in the file with the new values. But this does not
!happen and a new line is written.
但是请注意,这会覆盖从文件开头开始的内容,因此如果您在该文件中不仅有数组,它还会覆盖其他内容,可能会以某种方式使输出看起来像很奇怪。