Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran 77 - reading same file multiple times

  1. Jan 20, 2012 #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 (Text):

          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
     
  2. jcsd
  3. Jan 20, 2012 #2
    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:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran 77 - reading same file multiple times
  1. Fortran 77 (Replies: 1)

Loading...