Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the number of lines in Fortran 90 from a text file?

Tags:

fortran

How can I read the number of lines present in a text file?

My text file seems to be like:

1
2
3
.
.
.
n
like image 237
Adi Avatar asked Jun 07 '15 10:06

Adi


People also ask

How many characters is a line in Fortran?

4.1. In fixed-form source, lines can be longer than 72 characters, but everything beyond column 73 is ignored. Standard Fortran 95 only allows 72-character lines.

How do I change a line in Fortran?

In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with the following methods: If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line.


1 Answers

Use:

nlines = 0
OPEN (1, file = 'file.txt')
DO
    READ (1,*, END=10)
    nlines = nlines + 1
END DO
10 CLOSE (1)

print*, nlines

end

Or:

nlines = 0
OPEN (1, file = 'file.txt')
DO
  READ(1,*,iostat=io)
  IF (io/=0) EXIT
  nlines = nlines + 1
END DO
CLOSE (1)

print*, nlines
like image 90
prograils Avatar answered Nov 07 '22 18:11

prograils