Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Ifort and gfortran readig a file

Francisco_Pe__241_u_
397 Views

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")
    rewind(15)
    
    ios=0
    do while (ios == 0)
    read(15, * , iostat = ios )
    nrows = nrows + 1
    end do

    print*,nrows

end program nlines

0 Kudos
4 Replies
Steven_L_Intel1
Employee
397 Views
Please attach a data file that shows the problem. Don't paste it in, use the attach feature.
0 Kudos
Steven_L_Intel1
Employee
397 Views
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.
0 Kudos
Francisco_Pe__241_u_
397 Views
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. F.P
0 Kudos
Francisco_Pe__241_u_
397 Views
Thank you Steve. I have attached the right file. I solved the problem with the "if (ios == 0) nrows = nrows + 1" sentence.
0 Kudos
Reply