title: Unclear cause of "too many records in I/O statement" error
body: <p>While writing formatted output to a variable, I get the error</p>
  
  <pre><code>forrtl: severe (27): too many records in I/O statement
  </code></pre>
  
  <p>I've checked obvious things - whether data exceeds the format length and vice versa, number and type of variable are compatible with format specifiers - and I can't see why an error should be thrown.</p>
  
  <p>A minimal illustrative example is:</p>
  
  <pre><code>program TXTERR27
      use iso_fortran_env, only: OUTPUT_UNIT    
      implicit none
  
      double precision :: F1, F2
      integer :: I1
      character (len=25), parameter :: A1 = 'AaaaaAaaaaAaaaaAaaaaAaaaa'
  
      character (len=250) :: A2
  
  10  format(/, 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', I3, 'ZZZZZZZZZZZ',     &amp;
      F10.3, 1X, A25, /, 'ZZZZZZZZZZZZZZZZZ', F6.3, 'ZZZZZZZZ')
  20  format('Result :[', A250, ']')
  
      continue
  
      I1 = 1
      F1 = 1.0D0
      F2 = 1.0D-3
  
      write(OUTPUT_UNIT, *) "This works:"
      write(OUTPUT_UNIT, 10) I1, F1, A1, F2
      write(OUTPUT_UNIT, *)
  
      write(OUTPUT_UNIT, *) "This doesn't:"
      write(A2, 10) I1, F1, A1, F2
      write(OUTPUT_UNIT, 20) A2
  
      stop
  end program TXTERR27
  </code></pre>
  
  <p>This is compiled by using:</p>
  
  <pre><code>ifort -warn all -check all -traceback -o txterr27 txterr27.f90
  </code></pre>
  
  <p>and no warnings or errors result. The output is as follows:</p>
  
  <pre><code> This works:
  
  ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  1ZZZZZZZZZZZ     1.000 AaaaaAaaaaAaaaaAaaaaAaaaa
  ZZZZZZZZZZZZZZZZZ 0.001ZZZZZZZZ
  
   This doesn't:
  forrtl: severe (27): too many records in I/O statement, unit -5, file Internal Formatted Write
  Image              PC                Routine            Line        Source             
  txterr27           000000000046EEBE  Unknown               Unknown  Unknown
  txterr27           000000000046D956  Unknown               Unknown  Unknown
  txterr27           00000000004266A2  Unknown               Unknown  Unknown
  txterr27           0000000000403BBB  Unknown               Unknown  Unknown
  txterr27           0000000000403122  Unknown               Unknown  Unknown
  txterr27           0000000000419A44  Unknown               Unknown  Unknown
  txterr27           0000000000417BFC  Unknown               Unknown  Unknown
  txterr27           0000000000402586  MAIN__                     26  txterr27.f90
  txterr27           00000000004021DC  Unknown               Unknown  Unknown
  libc.so.6          00007F083D474EC5  Unknown               Unknown  Unknown
  txterr27           00000000004020D9  Unknown               Unknown  Unknown
  </code></pre>
  
  <p>To check if this is a compiler-specific problem, I rebuilt the example code under gfortran with:</p>
  
  <pre><code>gfortran -pedantic -Wall -fbacktrace -o txterr27 txterr27.f90                                                               
  </code></pre>
  
  <p>and got similar results:</p>
  
  <pre><code> This works:
  
  ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  1ZZZZZZZZZZZ     1.000 AaaaaAaaaaAaaaaAaaaaAaaaa
  ZZZZZZZZZZZZZZZZZ 0.001ZZZZZZZZ
  
   This doesn't:
  At line 26 of file txterr27.f90
  Fortran runtime error: End of file
  </code></pre>
  
  <p>Clearly there's no problem formatting the data, otherwise the write to stdout would fail. All I can think is that the <code>CRLF</code>s (<code>'/'</code>) are causing <code>write()</code> to render the result as an array not as a scalar. Writing an array to stdout would work fine (one entry per line) but it would fail if it was trying to write an array into a scalar.</p>
  
  <p>The error messages from both <code>gfortran</code> and <code>ifort</code> leave a lot to be desired;  with apologies to the late Douglas Adams: "<em>Mostly Useless</em>"</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Update:</strong> On continued analysis, the problem is my understanding of what constitutes a 'record' in Fortran. Here's an updated illustration:</p>
  
  <pre><code>program TXTERR27A
      use iso_fortran_env, only: OUTPUT_UNIT    
      implicit none
  
      double precision :: F1, F2
      integer :: I1
      character (len=25), parameter :: A1 = 'AaaaaAaaaaAaaaaAaaaaAaaaa'
  
      character (len=250) :: A2
      character (len=250), dimension(5) :: A3
  
  10  format(/, 'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ', I3, 'ZZZZZZZZZZZ',     &amp;
      F10.3, 1X, A25, /, 'ZZZZZZZZZZZZZZZZZ', F6.3, 'ZZZZZZZZ')
  20  format('Result: [', A250, ']')
  30  format('Result:', /, '[', *(A250, ']', /, '['))
  
      continue
  
      I1 = 1
      F1 = 1.0D0
      F2 = 1.0D-3
      A2 = ''
      A3 = ''
  
      write(OUTPUT_UNIT, *) "This works:"
      write(OUTPUT_UNIT, 10) I1, F1, A1, F2
      write(OUTPUT_UNIT, *)
  
      write(OUTPUT_UNIT, *) "Does this?"
      write(A3, 10) I1, F1, A1, F2
      write(OUTPUT_UNIT, 30) A3
  
      write(OUTPUT_UNIT, *) "This doesn't:"
      write(A2, 10) I1, F1, A1, F2
      write(OUTPUT_UNIT, 20) A2
  
      stop
  end program TXTERR27A
  </code></pre>
  
  <p>This example shows that multiple records/strings are emitted when writing to an internal variable and that the problem (as suspected) was that <code>write()</code> is trying to put an array into a scalar. This makes sense if you consider <code>'/'</code> as a record separator, not as a simple <code>CRLF</code>:</p>
  
  <pre><code> This works:
  
  ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  1ZZZZZZZZZZZ     1.000 AaaaaAaaaaAaaaaAaaaaAaaaa
  ZZZZZZZZZZZZZZZZZ 0.001ZZZZZZZZ
  
   Does this?
  Result:
  [                                                                                                                                                                                                                                                          ]
  [ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  1ZZZZZZZZZZZ     1.000 AaaaaAaaaaAaaaaAaaaaAaaaa                                                                                                                                                                         ]
  [ZZZZZZZZZZZZZZZZZ 0.001ZZZZZZZZ                                                                                                                                                                                                                           ]
  [                                                                                                                                                                                                                                                          ]
  [                                                                                                                                                                                                                                                          ]
  [
   This doesn't:
  At line 34 of file txterr27.f90
  Fortran runtime error: End of file
  </code></pre>
  
  <p>This problem is obvious if you keep in mind the archaic record-oriented nature of Fortran I/O. This is easy to forget after long experience with languages with text I/O facilities developed after, say, 1969. Apologies for not seeing this straightaway.</p>
  
  <p>Regardless, the important takeaway is to remember that <code>'/'</code> is a record separator and not look at it as Fortran's version of <code>\n</code>.</p>