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

Formatted stream access

IanH
Honored Contributor III
910 Views

I'm running into some oddities with formatted stream access (is that supported??). The attached dies when run under the VS debugger with a heap corruption report from the C library. When compiled and run from the command prompt (/check:all /warn:all) it appears to get a little confused. Larger versions of the code descend into merry chaos before going belly up.

(Compiled with 11.1.054 under VS2005. Change the filename in the open statement to be any text file that has more than two characters in the first line to see the problem).

[fortran]PROGRAM FormattedStream
  IMPLICIT NONE
  INTEGER, PARAMETER :: unit = 10
  CHARACTER :: c
  INTEGER :: stat
  !****
  OPEN( UNIT=unit, FILE='FormattedStream.f90', ACTION='READ',  &
      FORM='FORMATTED', ACCESS='STREAM', STATUS='OLD', IOSTAT=stat )
  IF (stat /= 0) STOP "Couldn't open the file :("
  READ(unit, 100, IOSTAT=stat, ADVANCE='NO') c
  IF (stat /= 0) STOP "Couldn't read the first char :("
  WRITE (*, 100) 'The first char was a "' // c // '" :)'
  READ(unit, 100, IOSTAT=stat, ADVANCE='NO') c
  IF (stat /= 0) STOP "Couldn't read the second char :("
  WRITE (*, 100) 'The second char was a "' // c // '" :)'
  CLOSE(unit)    
  100 FORMAT(A)
END PROGRAM FormattedStream
[/fortran]

0 Kudos
6 Replies
Steven_L_Intel1
Employee
910 Views
Amusing. I can reproduce this. Yes, stream formatted access is allowed. However, the more usual case is stream unformatted access if what you want to do is read character by character. Try that for now. I'll report the bug to the developers. Issue ID is DPD200150156.
0 Kudos
IanH
Honored Contributor III
910 Views

Thanks.

I don't particularly want to read character by character - I was just looking for a common (in compiler and platform sense) solution to the files that have an incomplete final record (as per recent discussion on c.l.f). Ideally I'd be reading into a buffer of some arbitrary size, and concatenating buffers together if they weren't big enough to hold the text in a particular record.

I think unformatted stream then means I have to deal with different line termination characters/sequences for different platforms?

0 Kudos
Steven_L_Intel1
Employee
910 Views

I suppose so, but the standard says:

36 (1) Some file storage units of the file may contain record markers; this imposes a record structure
37 on the file in addition to its stream structure. There might or might not be a record marker
38 at the end of the file. If there is no record marker at the end of the file, the final record is
39 incomplete.
0 Kudos
IanH
Honored Contributor III
910 Views
Well, character by character it is then!
0 Kudos
Steven_L_Intel1
Employee
910 Views
I expect the fix for this to appear in Update 6 (April).
0 Kudos
Steven_L_Intel1
Employee
910 Views
April showers bring Update 6 bug fixes, including this one.
0 Kudos
Reply