文件打开时记录错误结束 [英] End of record error in file opening

查看:884
本文介绍了文件打开时记录错误结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个模拟粒子碰撞的代码。我试图打开尽可能多的文件,因为有粒子(N),然后将时间积分的每个步骤(使用欧拉方法,但不相关)的每个文件中的位置和速度的数据。为此,我尝试使用do循环,以便打开我需要的所有文件 - 然后将所有数据放入其中,然后使用不同的do循环 - 然后关闭它们。

我第一次尝试做一个do循环来打开这些文件 - 但它给出了类型file已经在另一个单元中打开的错误,所以我做了以下操作:

 模块参数
隐式无
字符:: posvel
整数:: i,j ,N
real :: tmax
real,parameter :: tmin = 0.0,pi = 3.14159265,k = 500.0 * 10E3,dt = 10.0E-5,dx = 10.0E-3,g = 9.806 ,ro = 1.5 * 10E3
real,dimension(:),allocatable :: xold,xnew,vold,vnew,m,F,r
结束模块参数

程序碰撞
使用参数
implicit none

write(*,*)'输入粒子总数(整数):'
read(*,*)N

allocate(xold(N))
allocate(vold(N))
allocate(xnew(N))
allocate(vnew(N))
分配(m(N))
分配(F(N))
分配(r(N))

xold(1)= 0.0
vold 1)= 0.0
m(1)= 6.283 * 10E-9
r(1)= 10E-4

xold(2)= 5.0
vold(2) = 0.0
m(2)= 6.283 * 10E-9
r(2)= 10E-4

write(*,*)'输入模拟所花费的总时间(实数):'
read(*,*)tmax

do i = 1,N
write(posvel,(a,i3.3,a))posveldata,i,.txt
open(unit = i,file = posvel,status =unknown)
end do

do i = 1,N
close(unit = i)
end do

结束程序碰撞

最后十行是关于我的问题的。



在代码块中工作 - 它只打开我需要的文件数量,但我实际上使用gfortran,它在write语句中给我和记录结束错误。



如何让它正确执行并为我提供我需要的N个不同文件?



PS :这不是编译问题,而是执行程序后。 你的参数模块中的字符串只有1个字符长度,所以它不能包含完整的文件名。所以请使用更长的字符串,例如

  character(100):: posvel 

然后您可以打开每个文件

  (posvel,(a,i0,a))posveldata,i,.txt
open(unit = i,file = trim(posvel) ,status =unknown)
end do

在这里,我使用了格式i0自动确定整数的适当宽度,trim()用于删除文件名中不必要的空白(尽管它们可能不是必需的)。写语句也可以写得更紧凑,如

  write(posvel,('posveldata',i0,'。txt' ))通过在格式规范中嵌入字符文字,





错误消息记录结束来自上述问题。这可以通过下面的代码来确认:

 字符c 

写(c,(a) )1
print *,c =,c
write(c,(a))23!第8行test.f90
print *,c =,c

gfortran给出

  c = 1 
在文件test.f90的第8行
Fortran运行时错误:End记录

这意味着虽然 c 是用作内部文件,这个文件没有足够的空间容纳两个字符(这里是23)。为了比较,ifort14给出了:

  c = 1 
forrtl:severe(66):输出语句溢出记录,单位 - 5,文件内部格式化写入

Oracle Fortran12给出

  c = 1 
****** FORTRAN运行系统****** ******
错误1010:记录太长
位置:test.f90第8行的WRITE语句
中止

(有趣的是,Oracle Fortran报告记录太长,可能引用输入字符串。)


I am currently writing a code to simulate particle collisions. I am trying to open as much files as there are particles (N) and then put the data for positions and velocities in each of these files for each step of the time integration (using Euler's method, but that is not relevant). For that, I tried using a do loop so it will open all the files I need - then I put all the data in them with a different do loop later - and then close them all.

I first tried just doing a do loop to open the files - but it gave errors of the type "file already open in another unit", so I did the following:

module parameters
implicit none
character :: posvel
integer :: i, j, N
real :: tmax
real, parameter :: tmin=0.0, pi=3.14159265, k=500.0*10E3, dt=10.0E-5, dx=10.0E-3, g=9.806, ro=1.5*10E3
real, dimension(:), allocatable :: xold, xnew, vold, vnew, m, F, r
end module parameters

PROGRAM Collision
use parameters
implicit none

write(*,*) 'Enter total number of particles (integer number):'
read(*,*) N

allocate(xold(N))
allocate(vold(N))
allocate(xnew(N))
allocate(vnew(N))
allocate(m(N))
allocate(F(N))
allocate(r(N))

xold(1) = 0.0
vold(1) = 0.0
m(1) = 6.283*10E-9
r(1) = 10E-4

xold(2) = 5.0
vold(2) = 0.0
m(2) = 6.283*10E-9
r(2) = 10E-4

write(*,*) 'Type total time elapsed for the simulation(real number):'
read(*,*) tmax

do i = 1, N
    write(posvel,"(a,i3.3,a)") "posveldata",i,".txt"
    open(unit=i,file=posvel, status="unknown")
end do

do i = 1, N
    close(unit=i)
end do

END PROGRAM Collision

The last ten lines are the ones that regard to my problem.

That worked in codeblocks - it opened just the number of files I needed, but I'm actually using gfortran and it gives me and "end of record" error in the write statement.

How can I make it to execute properly and give me the N different files that I need?

P.S.: It is not a problem of compilation, but after I execute the program.

解决方案

Your character string in the parameter module has only 1 character length, so it cannot contain the full file name. So please use a longer string, for example

character(100) :: posvel

Then you can open each file as

do i = 1, N
    write(posvel,"(a,i0,a)") "posveldata",i,".txt"
    open(unit=i,file=trim(posvel), status="unknown")
end do

Here, I have used the format i0 to automatically determine a proper width for integer, and trim() for removing unnecessary blanks in the file name (though they may not be necessary). The write statement can also be written more compactly as

write(posvel,"('posveldata',i0,'.txt')") i

by embedding character literals into the format specification.


The error message "End of record" comes from the above issue. This can be confirmed by the following code

character c

write(c,"(a)") "1"
print *, "c = ", c
write(c,"(a)") "23"   !! line 8 in test.f90
print *, "c = ", c

for which gfortran gives

c = 1
At line 8 of file test.f90
Fortran runtime error: End of record

This means that while c is used as an internal file, this "file" does not have enough space to accommodate two characters (here "23"). For comparison, ifort14 gives

c = 1
forrtl: severe (66): output statement overflows record, unit -5, file Internal Formatted Write

while Oracle Fortran12 gives

c = 1
******  FORTRAN RUN-TIME SYSTEM  ******
Error 1010:  record too long
Location:  the WRITE statement at line 8 of "test.f90"
Aborted

(It is interesting that Oracle Fortran reports the record to be "too long", which may refer to the input string.)

这篇关于文件打开时记录错误结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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