Fortran77 end-of-file error reading formatted input

Click For Summary

Discussion Overview

The discussion revolves around reading formatted input from a file in Fortran77, specifically focusing on handling end-of-file conditions without generating errors. Participants explore methods to read six columns of data when the number of rows is not predetermined, and seek solutions for implementing this in a loop.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks to read a formatted file of six columns and wishes to avoid end-of-file errors when using a loop.
  • Another participant suggests using the END parameter in the READ statement, clarifying that the '999' should be a statement label, not data.
  • A participant expresses confusion over the syntax and the need to read an unknown number of rows, asking for a more elegant solution.
  • There is a mention of a syntax error encountered by the original poster, which was later attributed to variable type declaration issues.
  • Another participant notes that the loop does not execute, resulting in 'npoints' being zero, indicating a potential issue with the reading logic.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to handle the end-of-file condition without errors. There are multiple competing views on how to structure the code and handle the reading of data.

Contextual Notes

There are unresolved issues regarding the specific syntax and logic required to read the data correctly, as well as the handling of variable types and loop execution.

Who May Find This Useful

Individuals working with Fortran77, particularly those dealing with file I/O and data processing in scientific computing contexts.

gcc2012
Messages
3
Reaction score
0
Hi,

I am new to the forum, apologies if I break any rule posting a new thread. I could not find the answer in similar threads came up after searching the forum.

I want to read a formatted file of six column data. Reading works OK, but I want to put it into a do loop with the repeat as many as number of lines(rows) in the file and stops when reaching the end without complaining about the 'end of file'. I find in various sites that there is END parameter that can be used in READ, WRITE statements, e.g.,

READ (UNIT=15, FMT=2000, END=999)
and '999' at the end of file, but this does not work..

I am not so much into Fortran, so I do not understand the explanations of 'sequential', 'list directed', etc. exceptions to using END. And I do not know any other way of handling this.
Any help would be great!

gcc2012
 
Technology news on Phys.org
The 999 is a statement label in your program, not the data on the last line of the input file.

Do something like
Code:
do i = 1,n
  READ (UNIT=15, FMT=2000, END=999) a(i),b(i),c(i),d(i),e(i),f(i)
enddo
999 npoints = i - 1
where "npoints" will be the number of lines that you read from the file (or it will be equal to n, if the file contained more than n lines of data)
 
Thanks AlephZero, I tried that way and it gives syntax error.
How would you read the data file organized as follows:

- sequential formatted
- six columns of real numbers
- number of rows are not always known

My immediate goal is to get the data read and each column would form an individual array. It is easy if one knows the number of lines. But, I did not see any elegant way of getting the number of rows and using it for do loops later, or getting the data read until end-of-file and code continues to run without 'end of file' error.

any suggestion?
 
gcc2012 said:
I tried that way and it gives syntax error.
If you post the message, we might be able to help. If you don't, we can't.

How would you read the data file organized as follows:

- sequential formatted
- six columns of real numbers
- number of rows are not always known
I would do it the way I already posted.
 
Thanks AlephZero.

I sorted out the syntax error, it was due to the variable type declaration.

But, now I have another problem. It does not execute the do loop at all. I print out 'npoints' and the value is 0.

What would be wrong?

No error message in this case.

gcc2012
 
You need to post the program.
 

Similar threads

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