Getting Fortran to read CTRL/Z in a file as data - VMS

This is a discussion on Getting Fortran to read CTRL/Z in a file as data - VMS ; Greetings, I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried in a file as an end-of-file condition (I knew it took terminal CTRL/Z as EOF, but I had never realized it did the same for files). ...

+ Reply to Thread
Results 1 to 16 of 16

Thread: Getting Fortran to read CTRL/Z in a file as data

  1. Getting Fortran to read CTRL/Z in a file as data

    Greetings,

    I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    EOF, but I had never realized it did the same for files). Before I
    rewrite the Fortran READs as calls to $GET, is there an easier way to
    tell Fortran not to regard CTRL/Z as end-of-file ?

    Thanks

    ---------------------------------------------------------
    Tom Wade | EMail: tee dot wade at eurokom dot ie
    EuroKom | Tel: +353 (1) 296-9696
    A2, Nutgrove Office Park | Fax: +353 (1) 296-9697
    Rathfarnham | Disclaimer: This is not a disclaimer
    Dublin 14 | Tip: "Friends don't let friends do Unix !"
    Ireland

  2. Re: Getting Fortran to read CTRL/Z in a file as data

    On Nov 15, 8:50 am, Tom Wade wrote:
    > Greetings,
    >
    > I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    > in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    > EOF, but I had never realized it did the same for files). Before I
    > rewrite the Fortran READs as calls to $GET, is there an easier way to
    > tell Fortran not to regard CTRL/Z as end-of-file ?
    >
    > Thanks
    >
    > ---------------------------------------------------------
    > Tom Wade | EMail: tee dot wade at eurokom dot ie
    > EuroKom | Tel: +353 (1) 296-9696
    > A2, Nutgrove Office Park | Fax: +353 (1) 296-9697
    > Rathfarnham | Disclaimer: This is not a disclaimer
    > Dublin 14 | Tip: "Friends don't let friends do Unix !"
    > Ireland


    Tom,

    What is OPEN statement for this file? In particular, what options are
    you using to read this file?

    - Bob Gezelter, http://www.rlgsc.com

  3. Re: Getting Fortran to read CTRL/Z in a file as data

    Hi Tom

    After the fortran read has seen the EOF, try to read the next line. If
    that also is EOF, you found the real EOF (or the second ^Z in
    succession).
    If the next read gets normal data you can go on reading the file
    Example program below


    character*80 line
    open(1,file='x.x',status='old')
    c
    10 read(1,2000,end=20) nk,line
    2000 format(q,a)
    15 write(*,*) line(1:nk)
    goto 10
    c
    c We saw an EOF, if the next is again an EOF, all is done
    c
    20 write(*,*) 'Seen eof'
    read(1,2000,end=90) nk,line
    c
    c If not EOF again, keep on going
    c
    goto 15
    90 end

  4. Re: Getting Fortran to read CTRL/Z in a file as data

    In article <92654c6d-2a09-4823-86f9-80bd19182013@d50g2000hsf.googlegroups.com>, Bob Gezelter writes:
    > What is OPEN statement for this file? In particular, what options are
    > you using to read this file?


    It's easy enough to reproduce. A control-z on a line by itself in
    an RMS file is treated as EOF for the purpose of Fortran formatted I/O.

    options /extend_source

    implicit none

    integer recl
    character *512 record

    open ( unit=1, file='test.txt', status='old' )
    do while ( .true. )
    read ( 1, '(q,a)' ) recl, record
    type *, recl, record(1:recl)
    end do

    end

    $ type test.txt
    abc
    fdsa
    <^Z> (Fudged -- cut and paste didn't do justice to the reverse ?)
    asdf
    $ r test
    3 abc
    4 fdsa
    %FOR-F-ENDDURREA, end-of-file during read
    unit 1 file EISNER$DRA3:[DECUSERVE_USER.BRIGGS]TEST.TXT;20
    [... snip traceback ...]
    $

    This behavior comes as a shock to me. On VMS a control-Z is just a
    byte in a file. The following makes it pretty clear that the current
    behavior is incorrect.

    from http://www.cs-software.com/software/...q/relnotes.htm

    "The run-time support was improperly positioning before the last character in a
    file opened with form='binary' and position='append' if the character happened
    to be a control-z character, an achar(26). The run-time support was fixed to
    not skip the control-z under these conditions. (cvf11664)

    If you're going to append _after_ a control-Z then you obviously can't
    treat control-Z as end-of-file.

  5. Re: Getting Fortran to read CTRL/Z in a file as data


    > What is OPEN statement for this file? In particular, what options are
    > you using to read this file?


    Pretty minimal:

    Open (1, name='INPUT', readonly, status='old')

    ---------------------------------------------------------
    Tom Wade | EMail: tee dot wade at eurokom dot ie
    EuroKom | Tel: +353 (1) 296-9696
    A2, Nutgrove Office Park | Fax: +353 (1) 296-9697
    Rathfarnham | Disclaimer: This is not a disclaimer
    Dublin 14 | Tip: "Friends don't let friends do Unix !"
    Ireland

  6. Re: Getting Fortran to read CTRL/Z in a file as data


    > After the fortran read has seen the EOF, try to read the next line. If
    > that also is EOF, you found the real EOF (or the second ^Z in
    > succession).
    > If the next read gets normal data you can go on reading the file


    Yes, this certainly works. The problem is when you hit two or more
    CTRL/Zs in successive lines. How many EOFs do you take to be definitive?

    ---------------------------------------------------------
    Tom Wade | EMail: tee dot wade at eurokom dot ie
    EuroKom | Tel: +353 (1) 296-9696
    A2, Nutgrove Office Park | Fax: +353 (1) 296-9697
    Rathfarnham | Disclaimer: This is not a disclaimer
    Dublin 14 | Tip: "Friends don't let friends do Unix !"
    Ireland

  7. Re: Getting Fortran to read CTRL/Z in a file as data

    In article , Tom Wade writes:
    >
    >> After the fortran read has seen the EOF, try to read the next line. If
    >> that also is EOF, you found the real EOF (or the second ^Z in
    >> succession).
    >> If the next read gets normal data you can go on reading the file

    >
    > Yes, this certainly works. The problem is when you hit two or more
    > CTRL/Zs in successive lines. How many EOFs do you take to be definitive?


    Use ERRSNS.

    INTEGER io-err, sys-err, stat, unit, cond
    ...
    CALL ERRSNS ([io-err] [,sys-err] [,stat] [,unit] [,cond])

    After an EOF (real or fake), io-err will be 24. After a fake EOF,
    sys-err will be 0. After a real EOF, sys-err will be 98938 (RMS$_EOF)

  8. Re: Getting Fortran to read CTRL/Z in a file as data

    In article , briggs@encompasserve.org writes:
    > Use ERRSNS.
    >
    > INTEGER io-err, sys-err, stat, unit, cond
    > ...
    > CALL ERRSNS ([io-err] [,sys-err] [,stat] [,unit] [,cond])
    >
    > After an EOF (real or fake), io-err will be 24. After a fake EOF,
    > sys-err will be 0. After a real EOF, sys-err will be 98938 (RMS$_EOF)


    Following up my own post, here's proof of concept code:

    $ type test.for
    options /extend_source

    implicit none

    integer recl
    character *512 record

    character *8 pos

    integer ios, ioerr, syserr

    open ( unit=1, file='test.txt', status='old' )

    do while ( .true. )
    read ( 1, '(q,a)', iostat=IOS ) recl, record(1:recl)
    if ( ios .ne. 0 ) then
    call errsns ( ioerr, syserr )
    if ( ioerr .ne. 24 ) then
    stop 'Fatal I/O error'
    else if ( syserr .ne. 0 ) then
    go to 999 ! Real end of file
    else
    ! Fake end-of-file with control-Z record
    recl = 1
    record(1:1) = char(26)
    end if
    end if
    type *, recl, record(1:recl)
    end do

    999 continue ! Get here on actual physical EOF

    end
    $ type test.txt
    abc
    fdsa
    ?
    asdf
    $ r test
    3 abc
    4 fdsa
    1 ?
    4 asdf

    [The question mark is actually a control-Z]

  9. Re: Getting Fortran to read CTRL/Z in a file as data


    > Use ERRSNS.
    >
    > INTEGER io-err, sys-err, stat, unit, cond
    > ...
    > CALL ERRSNS ([io-err] [,sys-err] [,stat] [,unit] [,cond])
    >
    > After an EOF (real or fake), io-err will be 24. After a fake EOF,
    > sys-err will be 0. After a real EOF, sys-err will be 98938 (RMS$_EOF)


    Excellent - that does the job nicely. At the end= branch to the Fortran
    READ, I've added the ERRSNS check above to tell the difference.

    Many thanks

    ---------------------------------------------------------
    Tom Wade | EMail: tee dot wade at eurokom dot ie
    EuroKom | Tel: +353 (1) 296-9696
    A2, Nutgrove Office Park | Fax: +353 (1) 296-9697
    Rathfarnham | Disclaimer: This is not a disclaimer
    Dublin 14 | Tip: "Friends don't let friends do Unix !"
    Ireland

  10. Re: Getting Fortran to read CTRL/Z in a file as data

    On Nov 15, 8:50 am, Tom Wade wrote:

    > I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    > in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    > EOF, but I had never realized it did the same for files). Before I
    > rewrite the Fortran READs as calls to $GET, is there an easier way to
    > tell Fortran not to regard CTRL/Z as end-of-file ?


    A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    Fortran. You can, as others have noted, detect the endfile condition
    and keep reading. (This is a VMS Fortran extension - the Fortran
    standard does not allow this.)

    Steve (ex VMS Fortran developer)

  11. Re: Getting Fortran to read CTRL/Z in a file as data

    Steve Lionel wrote:
    > On Nov 15, 8:50 am, Tom Wade wrote:
    >
    >
    >>I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    >>in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    >>EOF, but I had never realized it did the same for files). Before I
    >>rewrite the Fortran READs as calls to $GET, is there an easier way to
    >>tell Fortran not to regard CTRL/Z as end-of-file ?

    >
    >
    > A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    > Fortran. You can, as others have noted, detect the endfile condition
    > and keep reading. (This is a VMS Fortran extension - the Fortran
    > standard does not allow this.)
    >
    > Steve (ex VMS Fortran developer)


    I'm a little surprised that VMS Fortran uses something other than the
    RMS end-of-file! I thought CTRL/Z was a DOS/Windows thing.


  12. Re: Getting Fortran to read CTRL/Z in a file as data

    Richard B. Gilbert wrote:
    > Steve Lionel wrote:
    >> On Nov 15, 8:50 am, Tom Wade wrote:
    >>
    >>
    >>> I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    >>> in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    >>> EOF, but I had never realized it did the same for files). Before I
    >>> rewrite the Fortran READs as calls to $GET, is there an easier way to
    >>> tell Fortran not to regard CTRL/Z as end-of-file ?

    >>
    >>
    >> A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    >> Fortran. You can, as others have noted, detect the endfile condition
    >> and keep reading. (This is a VMS Fortran extension - the Fortran
    >> standard does not allow this.)
    >>
    >> Steve (ex VMS Fortran developer)

    >
    > I'm a little surprised that VMS Fortran uses something other than the
    > RMS end-of-file! I thought CTRL/Z was a DOS/Windows thing.
    >

    No, it's an RT-11 thing 8-) that predates that " DOS/Windows thing."

    Jeff

  13. Re: Getting Fortran to read CTRL/Z in a file as data

    In article <47434BAC.7040005@comcast.net>, "Richard B. Gilbert" writes:
    > Steve Lionel wrote:
    >>
    >>
    >> A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    >> Fortran. You can, as others have noted, detect the endfile condition
    >> and keep reading. (This is a VMS Fortran extension - the Fortran
    >> standard does not allow this.)
    >>
    >> Steve (ex VMS Fortran developer)

    >
    > I'm a little surprised that VMS Fortran uses something other than the
    > RMS end-of-file! I thought CTRL/Z was a DOS/Windows thing.
    >


    ctrl/z is a VMS thing, actually pretty much a DEC thing long before
    VMS or DOS. DOS copied it.

    But I'm fairly surprised that there's a VMS Fortran extension that
    Steve didn't tell us how to turn off (can't be turned off?). We
    should be able to get ANSI behaviour without having to resort to
    coding workarounds with ERRSNS.



  14. Re: Getting Fortran to read CTRL/Z in a file as data

    Richard B. Gilbert wrote:
    > Steve Lionel wrote:
    >> On Nov 15, 8:50 am, Tom Wade wrote:
    >>
    >>
    >>> I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    >>> in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    >>> EOF, but I had never realized it did the same for files). Before I
    >>> rewrite the Fortran READs as calls to $GET, is there an easier way to
    >>> tell Fortran not to regard CTRL/Z as end-of-file ?

    >>
    >>
    >> A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    >> Fortran. You can, as others have noted, detect the endfile condition
    >> and keep reading. (This is a VMS Fortran extension - the Fortran
    >> standard does not allow this.)
    >>
    >> Steve (ex VMS Fortran developer)

    >
    > I'm a little surprised that VMS Fortran uses something other than the
    > RMS end-of-file! I thought CTRL/Z was a DOS/Windows thing.
    >

    It is not a DOS thing. ASCII ctrl-z is defined as an end
    of media marker going way back to paper tape and asynchronus
    communications. A ctrl-z code told a program to stop reading
    data from that source.

  15. Re: Getting Fortran to read CTRL/Z in a file as data

    In article , sol gongola writes:
    > Richard B. Gilbert wrote:
    >> Steve Lionel wrote:
    >>> On Nov 15, 8:50 am, Tom Wade wrote:
    >>>
    >>>
    >>>> I noticed that a Fortran program I wrote is interpreting a CTRL/Z buried
    >>>> in a file as an end-of-file condition (I knew it took terminal CTRL/Z as
    >>>> EOF, but I had never realized it did the same for files). Before I
    >>>> rewrite the Fortran READs as calls to $GET, is there an easier way to
    >>>> tell Fortran not to regard CTRL/Z as end-of-file ?
    >>>
    >>>
    >>> A one-byte CTRL-Z is how an ENDFILE record is implemented in VMS
    >>> Fortran. You can, as others have noted, detect the endfile condition
    >>> and keep reading. (This is a VMS Fortran extension - the Fortran
    >>> standard does not allow this.)
    >>>
    >>> Steve (ex VMS Fortran developer)

    >>
    >> I'm a little surprised that VMS Fortran uses something other than the
    >> RMS end-of-file! I thought CTRL/Z was a DOS/Windows thing.
    >>

    > It is not a DOS thing. ASCII ctrl-z is defined as an end
    > of media marker going way back to paper tape and asynchronus
    > communications. A ctrl-z code told a program to stop reading
    > data from that source.


    Modulo implementation details, it is FORTRAN thing.

    $ help fortran statement endfile

    FORTRAN

    Statements

    ENDFILE

    Writes an end-of-file record to a sequential file and positions the
    file after this record (the terminal point). Statement format:

    ENDFILE ([UNIT=]io-unit[,ERR=label][,IOSTAT=i-var])
    ENDFILE io-unit

    io-unit Is the logical unit specifier, optionally prefaced
    by UNIT=. UNIT= is required if unit is not the first
    I/O specifier.

    label Is the label of an executable statement that
    receives control if an error occurs, prefaced
    by ERR=.

    I don't know chapter and verse (presumably Steve Lionel does), but if
    the Fortran spec requires an end-of-file record as a concept distinct
    from physical end-of-file on the media and if you're going to emulate
    such a record under VMS, using a one byte record containing control-Z is
    as good a choice as any and better than most.

  16. Re: Getting Fortran to read CTRL/Z in a file as data

    On Nov 21, 1:19 pm, bri...@encompasserve.org wrote:

    > I don't know chapter and verse (presumably Steve Lionel does), but if
    > the Fortran spec requires an end-of-file record as a concept distinct
    > from physical end-of-file on the media and if you're going to emulate
    > such a record under VMS, using a one byte record containing control-Z is
    > as good a choice as any and better than most.


    The concept of an ENDFILE record derives from "tape marks" on magnetic
    tapes. Reading a tape-mark generates an end-of-file condition, but
    programs often were written to continue reading past these.

    In Fortran, an ENDFILE record has the appearance of an actual record -
    for example, you can BACKSPACE over it - but it need not be
    implemented by any physical encoding. I don't know the origins of the
    DEC extension of embedding ENDFILE records in a file - it predates me
    - but I know there are VMS programs that depend on it even today
    (about once or twice a year I see gripes from people moving to Intel
    Fortran from VMS and wanting this behavior, which we do support with
    an option.)

    The use of a single-byte CTRL-Z is fine for "SEGMENTED" unformatted
    files, as you can't write such a record in any other way. But for
    other record types, one can write such a record. If someone asked me
    today about this, I'd recommend against implementing such a feature
    for other record types.

    I wish I could suggest a compiler option for you to disable this, but
    I don't know of one for the VMS compilers. I don't think we had one
    back when I was working on them.

    Steve

+ Reply to Thread