Fortran 77 - reading same file multiple times

In summary, The speaker is using code written in F77, but they do not have time to change it. There is a section of code that is not working, but it has worked in the past. The code reads header details from a file and checks if there is enough space in the array before continuing to read the data. However, when the speaker tries to read the data in two separate read statements, it does not work. The issue is solved when they realize the data was originally written with two write statements, and now it is only one. The speaker concludes by saying they have learned something new.
  • #1
jf22901
55
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
  • #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:
 

1. How can I read the same file multiple times in Fortran 77?

In Fortran 77, you can use a DO loop to read the file multiple times. First, open the file using the OPEN statement. Then, use a DO loop to read the file multiple times, using the READ statement inside the loop. Finally, close the file using the CLOSE statement.

2. Can I use different variables to read the same file multiple times in Fortran 77?

Yes, you can use different variables to read the same file multiple times in Fortran 77. In each iteration of the DO loop, you can use a different variable to store the data read from the file.

3. How do I avoid overwriting the data when reading the same file multiple times in Fortran 77?

To avoid overwriting the data, you can use the INQUIRE statement to determine the size of the file before reading it. Then, use that size to allocate enough memory for the variables used to store the data. This way, the data will not be overwritten when reading the file multiple times.

4. Is it more efficient to read the file once and store the data or to read it multiple times in Fortran 77?

It is generally more efficient to read the file once and store the data, rather than reading it multiple times. This is because reading from a file is a time-consuming operation, and reading the file multiple times would take more time compared to reading it once and storing the data.

5. Can I use the same file handle to read the file multiple times in Fortran 77?

Yes, you can use the same file handle to read the file multiple times in Fortran 77. The file handle is just a reference to the file, and it can be used in multiple operations, such as opening, reading, and closing the file. However, it is good practice to close the file after each use and reopen it when needed.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top