Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!oliveb!sun!gorodish!guy From: guy@gorodish.UUCP Newsgroups: comp.unix.questions Subject: Re: Stdio flaws? Message-ID: <19977@sun.uucp> Date: Thu, 28-May-87 20:02:53 EDT Article-I.D.: sun.19977 Posted: Thu May 28 20:02:53 1987 Date-Received: Sat, 30-May-87 06:01:47 EDT References: <345@phoenix.PRINCETON.EDU> Sender: news@sun.uucp Lines: 34 > For some unknown reason, fflush(3) only flushes output buffers, > not input buffers. The reason is quite well known. "flushing" a stream means writing all buffered output to the underlying device. This has nothing to do with input. Purging an input buffer is a completely separate operation; I agree that it might be a good thing if standard I/O permitted you to do this, but it is emphatically NOT the operation that "fflush" should do. (No, please don't say "well, have 'fflush' do a purge on a stream opened for reading"; what about something open for reading *and* writing?) Another problem is that you may want to flush anything buffered *by the underlying device* as well; this would be a lot harder to do in a portable and device-independent fashion. > At step (3) you must truncate the file to zero length; rewinding the file > is not sufficient since this allows the previous contents of the file to > `stick out' beyond the end to the current contents. This may be accomplished > using `ftruncate (fileno (fp), 0)', however many systems do not support > ftruncate(2). Using something like `freopen (...)' is no good since you > do not know the name of the file - only an open file pointer is available! The fact that many systems do not support "ftruncate" should explain why the standard I/O library *can't* help you here. If you don't have a name for a file, there *is* no portable way to truncate it to zero length! What could standard I/O possibly do on a system without "ftruncate", or some other way to truncate a file given only a "handle" for the file (e.g. a file descriptor on UNIX), rather than a name? The standard I/O library is not capable of performing miracles.... Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com