Fortran Fortran 77 - reading same file multiple times

AI Thread Summary
The discussion revolves around a Fortran 77 code issue related to reading unformatted data files. The user encountered a problem where two sequential read statements failed to work correctly, resulting in an error message. The code attempts to read header details and then conditionally read additional data based on available space in an array. The user suspected that changes in the Fortran compiler version might have caused the issue. However, the problem was resolved when it was discovered that the original data files were created using two separate write statements for the header and data. In contrast, the user had mistakenly written both the header and data in a single write statement, leading to the read failure. This highlights the importance of matching the read and write operations in Fortran when dealing with unformatted files.
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:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
1K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
4
Views
1K
Replies
4
Views
2K
Back
Top