Ifort and gfortran readig a file

Ifort and gfortran readig a file

I appology if this place is not the appropiate place to ask my question.

Suppose that I have a file "sampleFile.dat" with 12 rows.

When i compile the below program with ifort i get 13.

When I compile the same program with gfortran i get 12.

Why i get differents values?

Thank you in advance.

program nlines
    implicit none
    integer :: nrows, ios
    nrows = 0
    open(unit=15, file ="sampleFile.dat")
    do while (ios == 0)
    read(15, * , iostat = ios )
    nrows = nrows + 1
    end do


end program nlines

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Please attach a data file that shows the problem. Don't paste it in, use the attach feature.

Retired 12/31/2016

I realized that if I add the sentence:
if (ios == 0) nrows = nrows + 1

then the ifort compiler gives the correct answerd but then the gfortran compiler does not.



Downloadtext/plain sdat.txt461 bytes

Your program executes 13 READs for 12 lines, and thus 13 increments of nrows. The last READ fails with an IOS of -1, but you've already changed nrows to 13. Your edit that tests ios is a correct fix. I don't know why gfortran gives a different result. The file you attached has many more than 12 rows.

Retired 12/31/2016

Thank you Steve.

I have attached the right file. I solved the problem with the "if (ios == 0) nrows = nrows + 1" sentence.

Leave a Comment

Please sign in to add a comment. Not a member? Join today