从文件末尾读取整数“FORMATTED数据传输完成时缺少格式” [英] Reading integer from file end with ``Missing format for FORMATTED data transfer done``

查看:994
本文介绍了从文件末尾读取整数“FORMATTED数据传输完成时缺少格式”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新一个Fortran程序来满足我的需求。该程序编译使用 f95 gcc 版本4.1.2(我知道这是旧的,但我不能更新它)。我想从一个文件中读取参数,并通过

  inquire(file =resLast,exist = resExist)
if(readStart.eq.1.and.resExist)then
open(unit = 18,file ='resLast',status ='old',action ='read')
read 18)startConf
read(18)avlength,stdlength,avenergy
read(18)i,h2(1)
read(18)i,h2(2)
read (18)i,h2(5)
read(18)i,h2(8)
read(18)i,h2(9) (18)i,h2(13)
read(18)i,h2(16)
read(18)i,h2(17) i,h2(18)
read(18)i,h2(20)
read(18)i,h2(25)
read(18)i,h2(32)
close(18)
else
startConf = 0
do i = 1,32
h2(i)= 0
comh2(i)= 0
enddo
avlength = 0d0
stdlength = 0d0
avenergy = 0d0
endif

输入文件看起来像

  0 
196.090732114834 38451.5752213317 53.4452884569457
1 9.188750409521163E-004
2 4.548226133920252E-004
4 8.704101492185146E -005
5 2.175445697503164E-004
8 4.992044341634028E-005
9 2.108949411194772E-005
13 4.304789035813883E-005
16 4.693072696682066E-005
17 8.976260141935199E-005
18 2.737747854553163E-005
20 5.637826689053327E-005
25 3.860405195155181E-005
32 3.193027441739105E-005

当我尝试运行这个时,我得到一个错误

  Fortran运行时错误:FORMATTED数据传输格式丢失
完成

指向第一个读取行。 startConf变量,以及 i``是一个整数。其余的变量是双精度的。

我曾经用这个方法将数据读入以前的数据(即使是相同的代码),没有问题,我现在的错在哪里?因为你不明确地指定它,你的打开语句假定一个格式化(文本)文件。阅读文本文件时,你必须提供一个格式:

  read(18,*)i1 

另外,如果你的文件是 unformatted 打开你的文件:

  open(unit = 18,file ='resLast',status ='old',action ='阅读',form ='unformatted')


I'm updating a Fortran program for my needs. The program compiles using f95 and gcc version 4.1.2 (I know it is old, but I can't update it). I want to read parameters from a file and I try this via

      inquire (file="resLast", exist=resExist)
      if (readStart.eq.1.and.resExist) then
         open (unit=18,file='resLast', status='old', action='read')
         read (18) startConf
         read (18) avlength, stdlength, avenergy
         read (18) i,h2(1)
         read (18) i,h2(2)
         read (18) i,h2(4)
         read (18) i,h2(5)
         read (18) i,h2(8)
         read (18) i,h2(9)
         read (18) i,h2(13)
         read (18) i,h2(16)
         read (18) i,h2(17)
         read (18) i,h2(18)
         read (18) i,h2(20)
         read (18) i,h2(25)
         read (18) i,h2(32)
         close (18)
      else
         startConf = 0
         do i=1,32
            h2(i)=0
            comh2(i)=0
         enddo
         avlength=0d0
         stdlength=0d0
         avenergy=0d0
      endif

The input file looks like

           0
   196.090732114834        38451.5752213317        53.4452884569457     
           1  9.188750409521163E-004
           2  4.548226133920252E-004
           4  8.704101492185146E-005
           5  2.175445697503164E-004
           8  4.992044341634028E-005
           9  2.108949411194772E-005
          13  4.304789035813883E-005
          16  4.693072696682066E-005
          17  8.976260141935199E-005
          18  2.737747854553163E-005
          20  5.637826689053327E-005
          25  3.860405195155181E-005
          32  3.193027441739105E-005

When I try to run this I get an error

Fortran runtime error: Missing format for FORMATTED data transfer
done

Which points to the first read line. The startConf variable, as well asi`` is an integer. The rest of the variables are double precision.

I've used this method to read data into integers previously (even in the same code) without problem, where is my current fault?

解决方案

As you do not specify it explicitely, your open statement assumes a formatted (text) file. When reading from a text file, you have to provide a format as well:

read(18, *) i1

Alternatively, if your file is unformatted (binary), open your file accordingly:

 open (unit=18, file='resLast', status='old', action='read', form='unformatted')

这篇关于从文件末尾读取整数“FORMATTED数据传输完成时缺少格式”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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