Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!princeton!phoenix!pupthy!lgy From: lgy@pupthy.UUCP Newsgroups: comp.unix.questions Subject: Stdio flaws? Message-ID: <345@phoenix.PRINCETON.EDU> Date: Thu, 28-May-87 14:10:58 EDT Article-I.D.: phoenix.345 Posted: Thu May 28 14:10:58 1987 Date-Received: Sat, 30-May-87 04:42:17 EDT Sender: news@phoenix.PRINCETON.EDU Reply-To: lgy@pupthy.PRINCETON.EDU (Larry Yaffe) Distribution: world Organization: Physics Dept, Princeton Univ Lines: 41 Summary: (1) flushing stdin, (2) truncating files There does not appear to be any clean/portable way to either (a) flush an input stream, such as stdin, or (b) truncate an existing, open file. By portable, I mean sufficiently portable to be used on non-Unix systems (e.g., Vax VMS or Cyber VSOS) which provide C and Unix compatible subroutine libraries for stdio level routines, but not for low-level I/O. In practice, this means section 3 routines, but not section 2. For some unknown reason, fflush(3) only flushes output buffers, not input buffers. The best solution I know uses: if (input = fdopen (dup (fileno (stdin)), "r")) fclose (stdin), *stdin = *input ; This is less than ideal due to the use of `dup' which can cause protability problems. Using something like `ioctl (0,TCFLSH,0)' is not a solution since stdin need not be coming from a terminal, and also since (as far as I know) stdio buffering is totally independent from whatever buffer this ioctl call refers to. Truncating a file also appears to be impossible using stdio level calls. To illustrate the problem consider trying to: 1) Create a temporary file using `tmpfile'. 2) Storing a line of text in this file, and later rewinding the file and rereading the text. 3) Storing a second line of text in the file, for later reuse. 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! I consider the inability to perform these tasks at the stdio level to be a significant flaw in the stdio package. If there are better ways to deal with these problems, please let me know. ------------------------------------------------------------------------ Laurence G. Yaffe Physics Dept ; Princeton Univ lgy@pupthy.PRINCETON.EDU PO Box 708 ; Princeton NJ 08544