Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 68030 - Last Review: December 1, 2003 - Revision: 2.0
FIX: Problem Reading Direct-Access File When RECL > BLOCKSIZE
This article was previously published under Q68030
An application that reads records from a file produces incorrect
results. This problem does not occur when the application is
compiled with Microsoft FORTRAN versions 4.0 or 4.0a for MS-DOS.
The application opens a direct access file with BLOCKSIZE set to a
value that is less than the RECL (record length).
Perform one of the following three methods to work around this
problem:
- Modify the source code to use a record length less than or equal
to the BLOCKSIZE (the default BLOCKSIZE value is 1024 bytes).
- Modify the source code to access the direct-access file with a
variable or group of variables, the length of which in bytes
matches the record length. This technique avoids partial record
access.
- Modify the source code to specify a REWIND statement following
any partial access to a record. The REWIND statement realigns
the file pointer to the beginning of the file. (The BACKSPACE
statement does not appear to solve this problem.)
Microsoft has confirmed this to be a problem in FORTRAN versions
4.01, 4.1, and 5.0 for MS-DOS and versions 4.1 and 5.0 for OS/2.
This problem was corrected in FORTRAN version 5.1 for MS-DOS and
OS/2.
The code sample below demonstrates this problem. The first DO-loop in
the application constructs a direct-access scratch file that contains
four records. Then the application performs a partial read of record 3
from the scratch file. Finally, a DO-loop reads the file from record 1
to record 4 and writes the results on the screen.
The program output indicates that the READ statement in the DO-loop
improperly accesses the information in the scratch file. Because the
RECL (record length) exceeds the BLOCKSIZE (buffer size), each READ
statement causes the buffer to fill twice. Because the extra
information is not flushed from the buffer, it is read into the ALINE
variable each time the code executes a READ statement after the
initial partial READ of record 3.
Sample Code
C Compile options needed: None
CHARACTER*514 ALINE
CHARACTER*10 SHORT
OPEN(9, ACCESS = 'DIRECT', RECL = 514,
+ FORM = 'FORMATTED', BLOCKSIZE = 512) ! NOTE: BLOCKSIZE < RECL
DO 10 J = 1, 4 ! Create a file to read.
ALINE = 'TEST ' // CHAR(#30 + J) ! Put the record number
WRITE(9, 30, REC = J) ALINE ! into each record.
10 CONTINUE
READ(9, 30, REC = 3) SHORT ! This is the partial READ
! of record 3.
DO 20 I = 1, 4
READ(9, 30, REC = I) ALINE
WRITE(*, *) ALINE
20 CONTINUE
30 FORMAT(A)
END
APPLIES TO
- Microsoft FORTRAN Compiler 4.01
- Microsoft FORTRAN Compiler 4.1
- Microsoft FORTRAN Compiler 5.0
- Microsoft FORTRAN Compiler 4.1
- Microsoft FORTRAN Compiler 5.0
Retired KB Content DisclaimerThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate