This is the mail archive of the mailing list for the GNU Fortran project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Stream-reading question


Assume I have a file (e.g. created via 'line1'//new_line('a')//'line2'):
what shall the following program print?

program stream
    implicit none
    character(len=50) :: r1,r2
    read(10,'(a)') r1
    read(10,'(a)') r2
    print *, ':',trim(r1),':'
    print *, ':',trim(r2),':'
end program stream

NAG f95 prints:

gfortran prints:

I think NAG f95 is right as I find in Fortran 2003, section 10.6.3:

"If the file is connected for stream access, the output may be split
across more than one record if it
 contains newline characters. A newline character is a nonblank
character returned by the intrinsic
 function NEW LINE. Beginning with the first character of the output
field, each character that is not
 a newline is written to the current record in successive positions;
each newline character causes file
 positioning at that point as if by slash editing (the current record is
terminated at that point, a new
 empty record is created following the current record, this new record
becomes the last and current record
 of the file, and the file is positioned at the beginning of this new

But I'm not 100% sure, whether I read this correctly (or whether this is
the right place in the standard).


PS: The actual test case I'm writing is:
! { dg-do run }
! Checks Fortran 2003's new_line intrinsic function
! PR fortran/28585
program new_line_check
    implicit none
    character(len=*), parameter :: rec1 = 'record1'
    character(len=*), parameter :: rec2 = 'record2'
    character(len=50) :: r1,r2
    integer           :: len

    write(10,'(a)') rec1//new_line('a')//rec2
! Shall this work or not?
!    rewind(10)
!    read(10,'(a)') r1
!    read(10,'(a)') r2
!    if(r1 /= rec1 .or. r2 /= rec2) call abort()

    read(10,'(a)') r1
    read(10,'(a)') r2
    if(r1 /= rec1 .or. r2 /= rec2) call abort()

    write(10) rec1//new_line('a')//rec2
    len = len_trim(rec1//new_line('a')//rec2)
    read(10) r1(1:len)
    if(r1 /= rec1//new_line('a')//rec2) call abort()
end program new_line_check

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]