Reading values from text file of unknown structure

Sorry if that was already asked, I could not find any relevant topic here or elsewhere. And ChatGPT has spectacularly failed on it.

Is there a simple, effective way of reading numerical values from a text file of unknown structure? I mean every line can contain a different number of values written in fields of different width.
ADVANCE=“no” could be an answer but it requires explicit format, so variable width excludes that. Surely one can read the whole line (suppose one can set a limit of max line length) and then parse it “manually”, but this is not simple, unless “SPLIT” intrinsic arrives (if I properly remember the name).

A list-directed read of array x will read values from each line and skip to the next line when the line is exhausted, until x is filled. For example the output with gfortran of the code

implicit none
real :: x(8)
open (unit=20, file="numbers.txt", action="read")
read (20, *) x
print*,x
end

for the file numbers.txt containing

1.1 2.1 3.1
4.1 5.1
6.1
7.1 8.1

is

   1.10000002       2.09999990       3.09999990       4.09999990       5.09999990       6.09999990       7.09999990       8.10000038    ```
1 Like

Well, yes. But if the last value read is not the last value in its line, the rest of the line will be skipped. So it would probably be better to read lines into a character variable and only then read from that variable as an internal file. That requires, however, setting a limit for the line length.

A thread about reading lines with unknown length was

Also, there
are examples on the Fortran Wiki related to this topic, including one that uses basic built-in Fortran
features:
https://fortranwiki.org/fortran/show/getvals

and readling lines of arbitrary length if that is really a problem instead of readling a line of some longer-than needed length like 1024 characters, etc. is in
https://fortranwiki.org/fortran/show/readline

If you use fpm there are several procedures for just that purpose such as s2vs() in

which is also available along with several similiar procedures in the simpler M_strings module as
well.

an example of a file (just number and whitespace or commas, semi-colons, …; all integers or floats as well, … would be useful if there are any known file attributes.

Thanks for all replies and hints. My question was, actually, whether Fortran itself does offer such a possibility. It seems, regrettably, it does not. Non-advancing input excludes list-directed format. Formatted STREAM input which, in principle, should provide such a functionality, for some reason does not either, working in a record-oriented way, contrary to the very idea of stream, what was already discussed here.