0

I'm trying to open a file a file that's contem a number a = 200 using function:

program test_func
    implicit none
    real :: Num,a
    open(10, file='tabelateste.txt', status='old')
    read(10,*)a
    print*, 'number in file'
    print*, a
    print*, Num
end program
real function Num()
    implicit none
    real :: a
    open(10, file='tabelateste.txt', status='old')
    read(10,*)a
    Num = a
end function 

the first print, run as expected, but the second no, what I'm doing wrong? a function in Fortran can read a file?

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
  • A function can read a file, but your `num` in the main program isn't the function `num`. Instead it's a scalar real variable (to which you don't assign a value). – francescalus Feb 18 '22 at 14:26
  • Even if you do call the function `num`, you'll hit the problem in the linked question: the `open` in the function does nothing to reposition the file (so you'll hit an end-of-file condition). – francescalus Feb 18 '22 at 14:32
  • You also can't call the function like `print*, num()`, because then you'd have [recursive I/O](https://stackoverflow.com/q/24923076/3157076). – francescalus Feb 18 '22 at 14:42
  • what is the correct way to call a function like that? with no "Unclassifiable statement" error? – Vinicius Fernandes Feb 18 '22 at 14:54

0 Answers0