Fortran 77 - reading same file multiple times

Click For Summary
SUMMARY

The forum discussion centers on an issue with reading an unformatted file in Fortran 77, specifically when using two separate read statements. The user, Jack, encountered a problem where the second read statement was skipped due to changes in how the data was written to the file. Initially, the data was written using two write statements, which allowed for successful reading with two read statements. However, after switching to a single write statement, the reading process failed. The solution involved reverting to the original method of writing data to ensure compatibility with the existing read logic.

PREREQUISITES
  • Understanding of Fortran 77 syntax and file I/O operations
  • Knowledge of unformatted file handling in Fortran
  • Familiarity with the Intel Fortran Compiler, specifically versions ifort v9 and ifort v12
  • Basic concepts of data structure and memory management in programming
NEXT STEPS
  • Review Fortran 77 file I/O documentation for unformatted files
  • Learn about the differences between write and read operations in Fortran
  • Explore best practices for managing data files in Fortran
  • Investigate version differences in Intel Fortran Compiler and their impact on code behavior
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those maintaining legacy code, as well as programmers dealing with file I/O operations in Fortran 77.

jf22901
Messages
55
Reaction score
1
Hi all

I am using some code that was originally written in F77, but as it is many thousands of lines long, I haven't the time to go changing it. There is one section of code that refuses to work, but I'm assuming it must have at some point since the code has been used in the past!

Basically there is a section of code that reads some header details from a file, checks if there is enough space left in the array, and if there is continues reading the data from the file. For example, say there was an unformatted data file containing 20 floating point values. The code is something like this:

Code:
      open(10, file='datafile', form='unformatted', status='old')

      ! Read the first part of the file
      read(10, end=2100) (header(i), i = 1, 10)

      ! Some conditional check to see if there is space for new data
      if (x .lt. max) then
         ! If there is space, continue reading the remaining data
         read(10, end=2100) (data(i), i = 1, 10)
      endif
      close(10)

2100  print*, 'FILE ERROR!'

However, putting the two read statements like this (one after the other) doesn't seem to work. If I read the file in one continuous go then it is ok, but if I leave in the conditional check, and try to read the remaining data with second read statement, it just skips to the error print statement.

Can anyone offer any help? As I say, this code must have worked in the past (I think I might have used it before myself), so I don't know why it doesn't now. Could it be because I've changed from ifort v9 to ifort v12?

Thanks,

Jack
 
Technology news on Phys.org
Aha! Problem solved.

Originally the data were written to the unformatted files with two write statements; one for the header information and one for the data. Thus, when Fortran came to read the data, the two read statements worked. When I created my datafile, I just wrote the header and data information with one write statement.

You live and learn... :smile:
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K