[comp.lang.c] Synchronizing buf & unbuf I/O

dhesi@bsu-cs.UUCP (Rahul Dhesi) (01/03/88)

I want to alternately do both buffered and unbuffered I/O on the same
file opened once only from a C program using stdio.  This means that
before switching between the two I must clear the stdio buffer and
synchronize the buffered and unbuffered seek pointers.

Before switching from buffered to unbuffered I/O, I can do

     fseek (f, ftell (f), 0);

and while this works for a file opened for read+write, it fails to
synchronize the seek pointers for a file opened read-only, under both
4.3BSD (VAX-11/785) and SVR2 (Microport System V/AT).  It works on
these systems if I follow the fseek() above with:

     lseek (fileno (f), ftell (f), 0);

But under MSDOS, this too fails to work with Microsoft C 3.0, although
it does work with Turbo C 1.0.

When switching back from unbuffered to buffered I/O I'm using

     fseek (f, lseek (fileno (f), 0L, 1), 0);

to bring the buffered seek pointer to the same position as the
unbuffered seek pointer and it seems to work.

I'm looking for alternative methods of known reliability.  The ideal
technique would work on most systems of any type that either have or
emulate the UNIX read() and write() system calls.  I realize that what
I'm trying to do is inherently unportable.

Note that the 4.3BSD documentation defines fflush() only for files that
are being written to.

Please post a response or email as you wish;  I will summarize any
significant email.
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!dhesi

peter@sugar.UUCP (Peter da Silva) (01/07/88)

How about this:

	switching from unbuffered to buffered I/O:

		int fd;
		FILE *fp;

		fp = fdopen(fd, mode);

	swithing from buffered to unbuffered:

		fd = dup(fileno(fp));
		fclose(fp);
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.