# Reading values from text file of unknown structure

**URL:** <https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091>\
**Category:** Help\
**Created:** [January 16, 2025, 4:08pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091 "2025-01-16T16:08:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [January 16, 2025, 4:08pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/1 "2025-01-16T16:08:58Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 16, 2025, 4:29pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/2 "2025-01-16T16:29:33Z")

</div>

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

```auto
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

```auto
1.1 2.1 3.1
4.1 5.1
6.1
7.1 8.1

```

is

````auto
   1.10000002 2.09999990 3.09999990 4.09999990 5.09999990 6.09999990 7.09999990 8.10000038 ```
````

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [January 16, 2025, 6:21pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/3 "2025-01-16T18:21:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 16, 2025, 6:46pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/4 "2025-01-16T18:46:57Z")

</div>

A thread about reading lines with unknown length was

> [@Reading lines from a file without padding](https://fortran-lang.discourse.group/t/reading-lines-from-a-file-without-padding/3088):
>
> I have long wondered how to read lines from a file in Fortran. The obvious character (len=1000) :: text ! connect file to unit iu read (iu,"(a)") text is often good enough but truncates lines that are longer than 1000 characters, and you don’t know if trailing blanks in text are actually in the file. Using a [code by Jacob Williams](https://community.intel.com/t5/Intel-Fortran-Compiler/Text-file-to-allocatable-string/td-p/1033082) to read a file into an allocatable string and then searching the string for newline characters, it is possible to recover the lines of a file. Here is the program r…

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 17, 2025, 2:59am UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/5 "2025-01-17T02:59:09Z")

</div>

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](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](https://fortranwiki.org/fortran/show/readline)

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

> **[GitHub - urbanjost/general-purpose-fortran: General Purpose Fortran Cooperative](https://github.com/urbanjost/general-purpose-fortran)**
>
> General Purpose Fortran Cooperative

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.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [January 17, 2025, 1:20pm UTC](https://fortran-lang.discourse.group/t/reading-values-from-text-file-of-unknown-structure/9091/6 "2025-01-17T13:20:28Z")

</div>

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](https://fortran-lang.discourse.group/t/formatted-stream-read/7248).
