Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Format error in reading numbers

Shrinivassan_R_
Beginner
4,838 Views

When the below command executes, it read the file content (    35) where all the spaces are ignored and it reads only the numbers in that case the size of the number is only 2 bytes, but we are trying to read 8 byte number which throws the exception.

 

FORMAT(I8)


IF I use FORMAT(I2) then there is no exception

 

      program test

 

 

      INTEGER(4) IRATE

 

      OPEN(4,file='C:\Srini\Files\Arat51',status='OLD',access='DIRECT',

     &               form='FORMATTED',recl=8)

 

 

      Read(4,200,rec=2)IRATE

 

      CLOSE(4)

 

200   FORMAT(I8)

 

      STOP

      end

 

0 Kudos
1 Solution
mecej4
Honored Contributor III
4,818 Views

With your data file, the following program works correctly if I specify RECL=10 in the OPEN statement, with the data file containing CR+LF at end-of-line. Similarly, with the datafile using LF for EOL, using RECL=9 in the OPEN statement gives correct output.

      program test
      INTEGER(4) IRATE,i
!
      OPEN(4,file='Arat51',status='OLD',access='DIRECT',
     &               form='FORMATTED',recl=10)
      i=2
      DO while(i .le.16)
         Read(4,200,rec=i)IRATE
         write(*,*)i,irate
         i=i*2
      end do
      CLOSE(4)
200   FORMAT(I8)
      STOP
      end

 

View solution in original post

0 Kudos
23 Replies
jimdempseyatthecove
Honored Contributor III
536 Views

Shrinivassan,

If you are processing data files produced elsewhere, you should be made aware that depending on the system where the file was written, and how the open declared the file, that the record delimiters can differ. In the first case you had CR+LF, a Linux/Unix system may use just LF, and other systems may have:    {byte count}data here{byte count},{byte count}data here{byte count},...
and there are several other variations on this.

For your program to be universal, in accepting formats written by various other systems you may need write a piece of code that determines the specific format.

Jim Dempsey

0 Kudos
Steve_Lionel
Honored Contributor III
536 Views

For a direct access file, you do indeed need to account for any metadata. As far as I know, if ifort writes a formatted, direct access file, it doesn't add any line delimiters (but I could be mistaken). If the file was created by something else, all bets are off.

0 Kudos
LRaim
New Contributor I
536 Views

Steve Lionel (Ret.) wrote:

For a direct access file, you do indeed need to account for any metadata. As far as I know, if ifort writes a formatted, direct access file, it doesn't add any line delimiters (but I could be mistaken). If the file was created by something else, all bets are off.


​I can confirm that direct access files produced with ifort do not include metadata. I read direct access files generated by ifort with MS C++ in a very transparent mode. One can also browse any file using an editor which allows hexadecimal view of each byte (e.g. the old IBM/SPF).

 

0 Kudos
Reply