How can I read the number of lines present in a text file?
My text file seems to be like:
1
2
3
.
.
.
n
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With