[comp.lang.c] fseek in VAX C

8099pierzina@vmsd.csd.mu.edu (Todd Pierzina) (07/11/90)

I need help.

I am using VAX C, and I want to move backward through a data (record, not
stream!) file.  I am trying to use fseek.  I have RTFMed, and it is very
ambiguous as far as fseek and record files go.  The Unix version would be

      (void) fseek(fp, (long)-sizeof(record_type), L_INCR);

where fp is the file pointer.  In VAX C, the RTL manual says that using fseek
with a value not obtained from ftell is very unpredictable.  No sh*t.

I could set up a stack of ftell values going forward, then when I need to go
back, pop the locations off and feed them to fseek directly.  I think.  So my
questions(s):

Is there an EASY way to move backward?

Will my stack method work?  I do not understand how fseek/ftell work with
record files; the manual is VERY confusing.

Please reply directly to me, as I am not subscribed to these lists.
Todd Pierzina

Student Programmer
Marquette University
8099PIER@MUCSD                (BITNET)
8099pierzina@vmsd.csd.mu.edu  (Internet)

daniels@tle.enet.dec.com (Bradford R. Daniels) (07/11/90)

What format is the file?  If it's a variable record length file, but the
records
are all the same size, you should be able to get to the previous by doing:

	fseek(fp, ftell(fp)-sizeof(record_type)-2,SEEK_SET)

(The -2 accounts for the 2-byte record size field.)  This work-around is,
of course, not supported, but it will get you there.

If the record format is fixed length, and the file's record size is the same
as sizeof(record_type), you can do:

	fseek(fp, ftell(fp)-sizeof(record_type),SEEK_SET)

Or even just make up an offset as long as it's a multiple of the file's
maximum record size.  Note that there's a good chance a future release
of VAX C will provide a way to position to arbitrary byte offsets in fixed
record length files.  Arbitrary positioning in variable record length file
is a much nastier problem, and one which may never satisfactorily be
resolved.

Of course, if it is a fixed format file, you can avoid the whole issue by
adding "ctx=stm" to your open() statement.  Your I/O will be slower,
but the Unix emulation is much better (including allowing arbitrary
byte positioning).

- Brad

-----------------------------------------------------------------
Brad Daniels			|  Digital Equipment Corp. almost
DEC Software Devo		|  definitely wouldn't approve of
"VAX C RTL Whipping Boy"	|  anything I say here...