C/UNIX low level I/O

Rob Bernardo rob at pbhyf.PacBell.COM
Thu Nov 3 02:37:34 AEST 1988


In article <6695 at pyr.gatech.EDU> david at pyr.gatech.edu (David Brown) writes:
+   I need to do a bunch of
+ small write's to a file.  I need to know the quickest way to do it.
...
+   For instance, the way I have it now, I use
+ write(2).  But this ay, I do a separate 'write' for every word in the
+ list, and a 'write' for the spaces between and the newlines. Like this:
+ 
+        while (list ~empty){
+         write word
+         write space
+        }
+        write newline
+
+ My question: is there a better way to do this?  I've thought of using 
+ higher-level I/O routines like fprintf, but I think they would be
+ less efficient. But less efficient than what I'm doing now?

Use fputs() to write the strings (your "word") and fputc() to write the
single characters (your "space" and "newline"). Fprintf() is overkill
because you don't have any formatting of the output to perform. Using
these stdio functions instead of the system call write() is more
efficient insofar as these functions buffer the writes, i.e. generally
they store what is to be written until a whole block is to be written, and
then they call write() to  write whole blocks at a time. (WARNING: that's
a simplification.)
-- 
Rob Bernardo, Pacific Bell UNIX/C Reusable Code Library
Email:     ...![backbone]!pacbell!pbhyf!rob   OR  rob at pbhyf.PacBell.COM
Office:    (415) 823-2417  Room 4E750A, San Ramon Valley Administrative Center
Residence: (415) 827-4301  R Bar JB, Concord, California



More information about the Comp.unix.questions mailing list