Non-advancing read in Fortran with free format
Asked Answered
T

1

8

I want to read a line in a file, which includes three real numbers, without advancing the pointer. So I wrote: (TXT is the variable representing my file which has a value of 80)

read(TXT, *, ADVANCE='NO') (numbers(i),i=1,3)

However, I got an error message saying:

"error #6568: This use of the ADVANCE, SIZE, or EOR specifier is invalid."

So how should I write it to make it correct?

Thanks.

Transude answered 24/6, 2014 at 2:39 Comment(1)
If the reason you don't want to advance to the next line is that there is more data to read on the line, a common approach is to read the whole line into a string using the "(a)" format and then repeatedly read from the string.Bossuet
D
10

You can use advance='no' only with an explicit format. The reason is the following : advance='no' just avoids to go to the next record (notice that the file pointer advances anyway, just after the last read value); but with a directed list (format *), one doesn't know how many record are involved by your read statement (the three numbers may be written on four lines for instance).

Dugger answered 24/6, 2014 at 3:31 Comment(1)
Also note that ADVANCE can only be used for reading from an external unit. I got burned trying to read from a character array.Exult

© 2022 - 2024 — McMap. All rights reserved.