[comp.os.vms] ftell & fseek

FELICE@AGR03.TO.CNR.IT ("Felice G. Marchesi * +39-39-6555.563 *") (07/28/88)

Dear Info-Vaxers,

I'm forwarding to you this question for one of my friends.

Please reply directly to him to the address:

 VOLPONES@AGR04.TO.CNR.IT

     ----------------- original message follows ---------------------

From : Volpones Carlo

I was working on adapting a C program, currently running under UNIX, to VMS.
I had some problems with the use of "ftell" and "fseek" .
After a call to "fseek" and a number 'n' of calls to "fgetc" are done,
the next "ftell" don't return the actual position ( n bytes after the last
"fseek"), but return the position of the last "fseek".
I think that could be some problems into the implementation of "fseek" and
"getc" so that the pointer to the position into the file is not correctly
handled by this functions.

-----------------------------------------------------------------------------

Felice Marchesi         Felice@agr03.to.cnr.it

LEICHTER@VENUS.YCC.YALE.EDU ("Jerry Leichter ", LEICHTER-JERRY@CS.YALE.EDU) (08/01/88)

	I was working on adapting a C program, currently running under UNIX,
	to VMS.  I had some problems with the use of "ftell" and "fseek" .
	After a call to "fseek" and a number 'n' of calls to "fgetc" are done,
	the next "ftell" don't return the actual position ( n bytes after the
	last "fseek"), but return the position of the last "fseek".  I think
	that could be some problems into the implementation of "fseek" and
	"getc" so that the pointer to the position into the file is not
	correctly handled by this functions.

On VMS, files may be record- or stream-oriented.  A record-oriented file,
which is what you get by default, permits seeking only to the beginning of
a record.

This is fully discussed in the VAX C documentation.

							-- Jerry

carl@CITHEX.CALTECH.EDU (Carl J Lydick) (08/01/88)

First, I'm sending this to INFO-VAX because my system never heard of the
node AGR04.TO.CNR.IT.

 > I was working on adapting a C program, currently  running  under  UNIX,  to
 > VMS.   I  had  some problems with the use of "ftell" and "fseek" .  After a
 > call to "fseek" and a number 'n' of calls to "fgetc"  are  done,  the  next
 > "ftell" don't return the actual position ( n bytes after the last "fseek"),
 > but return the position of the last "fseek".  I think that  could  be  some
 > problems  into the implementation of "fseek" and "getc" so that the pointer
 > to the position into the file is not correctly handled by this functions.

In section 2.5.3 of the VAX-C Run-Time  Library  Reference  Manual,  ftell  is
documented as follows:
        The ftell function measures the offset from the  beginning  of
        the  file.   With  record  files,  ftell  returns the starting
        position of the current record, not the current byte offset.
I think that the category "record files" doesn't  include  stream  files.   It
should  also  be  noted  that,  except  for  the first record of a file, ftell
considers the current record to be the last record from which data  was  read;
this  means  that  if you read one record in its entirety, do an ftell then an
fseek, you'll be back at the beginning of the record you just  read.  You  can
get around this by doing an ungetc(getc()) just before the call to ftell.

jdc@naucse.UUCP (John Campbell) (08/04/88)

From article <8808031204.AA26428@ucbvax.berkeley.edu>, by LEICHTER@VENUS.YCC.YALE.EDU ("Jerry Leichter ", LEICHTER-JERRY@CS.YALE.EDU):
> 
> 	I was working on adapting a C program, currently running under UNIX,
> 	to VMS.  I had some problems with the use of "ftell" and "fseek" .
> 	After a call to "fseek" and a number 'n' of calls to "fgetc" are done,
> 	the next "ftell" don't return the actual position ( n bytes after the
> 	last "fseek"), but return the position of the last "fseek".  I think
> 	that could be some problems into the implementation of "fseek" and
> 	"getc" so that the pointer to the position into the file is not
> 	correctly handled by this functions.
> 
> On VMS, files may be record- or stream-oriented.  A record-oriented file,
> which is what you get by default, permits seeking only to the beginning of
> a record.
> 
> This is fully discussed in the VAX C documentation.
> 
> 							-- Jerry

Jerry has helped me a lot in the past (probably without remembering doing
so) and I don't want this comment to be construed as any criticism at all,
but...

There is a problem with ftell in VMS 'C' which thinking of things as record
oriented doesn't intuitively explain.  This "feature" has been discussed
on the net before--namely an fgets followed by an ftell (which all sounds
record-like) won't properly position you to the start of the record you
expect unless the file is a stream-lf file.  Current solutions include
doing an ungetc(fgetc(...)) prior to the ftell.  The first time I ran into
this I did not feel it was "fully discussed in the VAX C documentation".

As always, if you don't like this behavior, please submit an SPR to DEC
(I finally got an acknowledgement that they received one of the two I sent
in on this).
-- 
	John Campbell               ...!arizona!naucse!jdc

	unix?  Sure send me a dozen, all different colors.