4

I am trying to read the integers in this line:

# 14 14 10

in Fortran 2008.

I attempted to use this code:

read(21, "(A, I,I,I)") garbage, a, b, c

but this is not standard conforming. Intel Fortran issues a warning "Fortran 2008 does not allow this edit descriptor. [I]" and other qeustions explain this problem: Nonnegative width required in format string Error: Nonnegative width required in format string at (1)

How do I properly read the integers of unknown width using Fortran 2008? I can not simply specify I2, because I do not know the width of the integer in advance.

Stein
  • 3,179
  • 5
  • 27
  • 51
  • 1
    Please use tag [tag:fortran] for all Fortran quations. Use version tags in addition for version specific questions. Is there any reason for the fortran95 tag? – Vladimir F Героям слава Oct 04 '17 at 07:59
  • You cannot use just I, it is not 2008 specific, it is true in every version. You are just probably asking strict Fortran 2008 compliance even though you do not show it. But it is true.in the past versions as well. It is a duplicate, please retag the question with the [tag:fortran] tag. – Vladimir F Героям слава Oct 04 '17 at 08:01
  • Does this mean I need to know the width of the Integer in advance? – Stein Oct 04 '17 at 08:03
  • With the duplicate I have to disagree. The question marked deals with write statements. I want to read an integer of unknown width. – Stein Oct 04 '17 at 08:05
  • Yes you do. You can use list directed read in many cases `read(21,*)`. – Vladimir F Героям слава Oct 04 '17 at 08:07
  • Here you have case with `read`. https://stackoverflow.com/questions/41287825/nonnegative-width-required-in-format-string?noredirect=1&lq=1 There are many more of these. Go to the duplicate and check the **Linked** questions. It is the same for read and write. – Vladimir F Героям слава Oct 04 '17 at 08:09
  • Here you have the same error in `FORMAT`, irrespective, whether used in read or write https://stackoverflow.com/questions/44860322/gfortran-requires-format-widths-while-ifort-doesnt?noredirect=1&lq=1 The `I` descriptor always requires a non-negative width. – Vladimir F Героям слава Oct 04 '17 at 08:10
  • I really think `read(21,*) garbage, a, b, c` should work just fine and it is the normal thing one should always start with. Maybe you are used from C to always require a format string, but in Fortran we often use the list directed formatting with `*`. Just be careful, that list-directed read can continue reading on the next line if there are not enough items in the current line. – Vladimir F Героям слава Oct 04 '17 at 10:39
  • I re-opened the question after I edited it so that it changes focus from the error messages from the attempted solution to the question for another way. – Vladimir F Героям слава Oct 06 '17 at 15:59

1 Answers1

3

As I hinted in the comments you can read items like this easily with the list directed I/O. The compiler then itself identifies which characters belong to each item in the input list and parse them. The items can be separated by spaces, commas or also a newline.

read(21,*) garbage, a, b, c

This is the most common way to read stuff interactively, but is also useful for parsing lists in data files (CSV and similar).

If on of the numbers were not present in the input record (line in a text file), the reading would continue on the next record.