Help needed -> fseek() and ftell() in VMS VAX C

dlnash at ut-ngp.UUCP dlnash at ut-ngp.UUCP
Fri Feb 13 03:25:26 AEST 1987


In article <384 at umnd-cs-gw.umnd-cs.UUCP>, jwabik at umnd-cs.UUCP (Jeff Wabik) writes:
> I'm trying to fseek my way around a large (1.5MB) file using VAX VMS C,
> and am having absolutely NO LUCK.  
> 
> Precise Problem Statement:
> 
> 	1)  File is opened properly. 
> 	2)  I've converted LONG ftell variables from UNIX to INT 
> 	    ftell variables for VMS.  I've followed other instructions
> 	    in the manual as well.
> 	3)  fseek(fp,n,0) does NOT bring me to byte n in the file....
> 

VAX C manual p. 16-21:  "With record files, an fseek to a position that
was not returned by ftell causes unpredictable behavior."  I also tripped
over this problem.  If the file you are trying to fseek around in is not
a Stream_LF file, then fseek will only seek to record boundaries, not
character positions, or something broken like that.  If you brought the
data file over from another system, then it is probably not in Stream_LF
format.  The only way around this problem is to write a program which
does something like:

        while ((c = fgetc(in)) != EOF)
            fputc(c, out);

to make a copy of the file in Stream_LF format (which the stdio library
creates by default).  Then try fseeking around in the copy.  It's a
kludge, but it works.  BTW, don't rely on feof, it is broken too.  If
you read the last byte from a file, so that the next read will return
EOF, feof will not indicate the eof condition.  If you try to read and
get EOF, then try feof, it will indicate the eof condition.  That in my
mind is broken.  It should indicate eof when there is nothing left to
read, not after a read returns EOF. 

Oh, one more thing, make sure the variable 'c' in the above example is
an int, not a char.  The character 0xff is a perfectly valid character
on a machine with an 8-bit character set, especially if your are reading
and writing files with binary data in them.  Fgetc will return 0x000000ff
if the character it reads is 0xff.  The constant EOF is 0xffffffff.  The
above loop will work correctly only if 'c' is an int, not a char.

				Don Nash

UUCP:    ...!{ihnp4, allegra, seismo!ut-sally}!ut-ngp!dlnash
ARPA:    dlnash at ngp.CC.UTEXAS.EDU
BITNET:	 CCEU001 at UTADNX, DLNASH at UTADNX

UUU       UUU
 U         U                The University of Texas at Austin
 U     TTTTUTTTTTTTTT              Computation Center
 U     T   U TT     T
  U       U  TT            "The world is basically non-linear."
   UUUUUUU   TT
             TT 
            TTTT



More information about the Comp.lang.c mailing list