Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help for handling EOF in Fortran95

Status
Not open for further replies.

Flutan

Technical User
Oct 4, 2012
1
SE
Hello everyone,

I'm a beginner of Fortran95, but with some experience with Fortran77.

In my programming recently, I encounter a problem with READ in Fortran95.
If I read the last line of a data file, the code will report it reaches EOF and disgardes the last number.

For example, This is my program:

OPEN (UNIT=7,FILE='FILNAME.dat')
READ (7,*,end=10) a, b
10 continue
WRITE(*,*) a, b
ENDDO

And this is data file (FILNAME.dat):

100 111
2 1
30 31(EOF)


The program will print like:

100 111
2 1
30 1

I tried to compile this part in Fortran77, and it works fine.
However in Fortran95, it disgards the last record. But unfortunately the whole program has to be compiled in Fortran95.
So is there any method to solve this problem?

Thank you a lot!!!!
 
Yes I know about this problem. It's compiler dependent and it happens with gfortran.
For example:
Code:
$ g77 -ffree-form flutan.f -o flutan
$ flutan
 100 111
 2 1
 30 31

$ gfortran flutan.f95 -o flutan
$ flutan
         100         111
           2           1

$ g95 flutan.f95 -o flutan
$ flutan
 100 111
 2 1
 30 31

What helps for gfortran with this problem is to insert END-OF-LINE at the last line of the data file, i.e instead of
this
Code:
100 111
2 1
30 31#EOF
you should have this
Code:
100 111
2 1
30 31
#EOF
Then gfortran reads the data without problems
Code:
$ gfortran flutan.f95 -o flutan
$ flutan
         100         111
           2           1
          30          31

P.S.: The source I used above is here:
Code:
program flutan
  integer :: a, b

  open (unit = 10, file = 'flutan.txt', status = 'unknown')

  do while (.true.)
     ! read line from the file
     read (10, *, end=99) a, b
     write (*,*) a, b
  end do

  99 continue ! end of file

  close(10)
end program flutan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top