This is the mail archive of the fortran@gcc.gnu.org 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


Hello,

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

program stream
    implicit none
    character(len=50) :: r1,r2
    open(10,file='test.dat',form='formatted',access='stream',status='old')
    read(10,'(a)') r1
    read(10,'(a)') r2
    print *, ':',trim(r1),':'
    print *, ':',trim(r2),':'
end program stream


NAG f95 prints:
---------------
 :line1:
 :line2:
---------------

gfortran prints:
---------------
 :line1
line2
:
 ::
---------------

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
record)."

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

Tobias

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

    open(10,file="test.dat",form='formatted',access='stream',status='new')
    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()
    close(10)

   
open(10,file="test.dat",form='formatted',access='sequential',status='old')
    read(10,'(a)') r1
    read(10,'(a)') r2
    close(10,status='delete')
    if(r1 /= rec1 .or. r2 /= rec2) call abort()

    open(unit=10,form='unformatted',access='stream',status='scratch')
    write(10) rec1//new_line('a')//rec2
    len = len_trim(rec1//new_line('a')//rec2)
    rewind(10)
    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]