3
votes

This should be quite simple, but I can't manage to read in a floating point number in Fortran. My program test.f looks like this:

  PROGRAM TEST
  open(UNIT=1,FILE='test.inp')
  read(1,'(f3.0)')line

  STOP
  END

The input file test.inp simply contains a single float: 1.2

Now the compiling of my testfile goes fine, but when I run it I get an error:

At line 4 of file test.f (unit = 1, file = 'test.inp')

Fortran runtime error: Expected REAL for item 1 in formatted transfer, got INTEGER

(f3.0)

^

I've tried different modifications of the code and also googling for the error message, but with no result. Any help would be greatly appreciated!

Regards, Frank

3
Whilst this doesn't answer the question, you should really declare the type of all variables you use. In your code nowhere do you state the type of line. It is good practice to include the line implicit none at the top of a program, function, subroutine or module (before any use statements). Implicit type declarations, which is what you have used, can cause lots of nasty, hard to find bugs. - Chris
This actually does answer the question. - Vladimir F

3 Answers

5
votes

Your variable line is implicitly defined as integer. This doesn't work with thef edit descriptor. If you want to read an integer use i edit descriptor (i3 for example). Otherwise declare line as real to math the "f" descriptor.

Note beside: the .0 is not a problem, because if Fortran gets a number with decimal point the .0 part in the descriptor is ignored. It is only used when an number without a decimal is entered and then it uses the number behind the decimal point in the desciptor to add a decimal point into the right place. For with F8.5, 123456789 is read as 123.45678. More ont this here http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/lref_for/source_files/pghredf.htm .

1
votes

In your read statement

read(1,'(f3.0)')line

the f3.0 tells tour program to read 3 digits with 0 digits after the decimal (this is what the n.m syntax means). So I presume that the program is just reading 1 from the file (not 1.2), which is an integer. Try replacing that line with something like

read(1,'(f3.1)')line

although, if the number in your file is likely to change and be larger than 9.9 or have more than one decimal place you should increase the field width to something larger than 3.

See the documentation of the read intrinsic and for data edit descriptors for more information on reading and writing in Fortran.

Edit: the format specifier, the second argument in quotes in your read statment, has the form fw.d, where f indicates that the data to read is a floating point number, w is the width of the field including all blanks and decimal points and d specifies the number of digits to the right of the decimal point.

1
votes

I would suggest reading/writing list formatted data, unless you have a very strong reason to do otherwise. assuming that you're reading in from a file with just a single float or integer in a single line, like this

123.45
11
42

then this should do the reading

real*8 :: x,y,z
open(1,file=filename)
read(1,*)x
read(1,*)y
read(1,*)z
close(1)