PRB: READ of Alphanumeric Padded with Blanks or Random Data

Last reviewed: December 11, 1995
Article ID: Q41019
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0 and 1.0a
  • Microsoft FORTRAN PowerStation 32 for Windows NT, version 1.0 and 4.0

SYMPTOMS

In a FORTRAN READ statement, an alphanumeric variable is padded with blank characters or random data.

CAUSE

In the READ statement format string, the length specified in the A format descriptor is longer than the width of the associated character variable.

RESOLUTION

Specify a smaller length value with the A format descriptor.

MORE INFORMATION

Truncating the input field is the behavior specified in the "American National Standard, Programming Language FORTRAN" document, approved April 3, 1978. The ANSI FORTRAN-77 specification includes the following statement on Page 13-13:

   Let len be the length of the input/output list item. If the specified
   field width w for A input is greater than or equal to len, the rightmost
   len characters will be taken from the input field.

In other words, if the format width for an alphanumeric is greater than the variable in which the data are stored, the READ begins at character position (width - variable_length + 1). Therefore, as the example below demonstrates, if the input record is shorter than the specified format width, the variable includes blank characters or other random data.

The following sample data file and application source code demonstrate this behavior.

Sample Data File TEST.DAT

thisisthefirstlineofthefile thisisthesecondlineofthefile thisisthethirdlineofthefile thisisthelastlineofthefile

Sample Code

C Compiler options needed: None

      CHARACTER*20 STUFF
      OPEN(UNIT = 1, FILE = 'TEST.DAT')
      READ(1, '(A100)') STUFF
      WRITE(*, *) STUFF
      READ(1, '(A10)') STUFF
      WRITE(*, *) STUFF
      READ(1, '(A25)') STUFF
      WRITE(*, *) STUFF
      READ(1, '(A)') STUFF
      WRITE(*, *) STUFF
      CLOSE(1)
      STOP
      END

The sample application produces the following output. The first line is blank because the program reads input beginning at column (100 - 20 + 1), column 81. The second line receives the first 10 characters. The third line begins at column (25 - 20 + 1), column 6. The fourth line receives the first 20 characters. The output is as follows:

   <blank line>
   thisisthes
   sthethirdlineofthefi
   thisisthelastlineoft


Additional reference words: 1.00 4.00 4.01 4.10 5.00 5.10
KBCategory: kbprg kbprb
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.