| <html lang="en"> |
| <head> |
| <title>Streams and File Descriptors - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="I_002fO-Concepts.html#I_002fO-Concepts" title="I/O Concepts"> |
| <link rel="next" href="File-Position.html#File-Position" title="File Position"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Streams-and-File-Descriptors"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="File-Position.html#File-Position">File Position</a>, |
| Up: <a rel="up" accesskey="u" href="I_002fO-Concepts.html#I_002fO-Concepts">I/O Concepts</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">11.1.1 Streams and File Descriptors</h4> |
| |
| <p>When you want to do input or output to a file, you have a choice of two |
| basic mechanisms for representing the connection between your program |
| and the file: file descriptors and streams. File descriptors are |
| represented as objects of type <code>int</code>, while streams are represented |
| as <code>FILE *</code> objects. |
| |
| <p>File descriptors provide a primitive, low-level interface to input and |
| output operations. Both file descriptors and streams can represent a |
| connection to a device (such as a terminal), or a pipe or socket for |
| communicating with another process, as well as a normal file. But, if |
| you want to do control operations that are specific to a particular kind |
| of device, you must use a file descriptor; there are no facilities to |
| use streams in this way. You must also use file descriptors if your |
| program needs to do input or output in special modes, such as |
| nonblocking (or polled) input (see <a href="File-Status-Flags.html#File-Status-Flags">File Status Flags</a>). |
| |
| <p>Streams provide a higher-level interface, layered on top of the |
| primitive file descriptor facilities. The stream interface treats all |
| kinds of files pretty much alike—the sole exception being the three |
| styles of buffering that you can choose (see <a href="Stream-Buffering.html#Stream-Buffering">Stream Buffering</a>). |
| |
| <p>The main advantage of using the stream interface is that the set of |
| functions for performing actual input and output operations (as opposed |
| to control operations) on streams is much richer and more powerful than |
| the corresponding facilities for file descriptors. The file descriptor |
| interface provides only simple functions for transferring blocks of |
| characters, but the stream interface also provides powerful formatted |
| input and output functions (<code>printf</code> and <code>scanf</code>) as well as |
| functions for character- and line-oriented input and output. |
| <!-- !!! glibc has dprintf, which lets you do printf on an fd. --> |
| |
| <p>Since streams are implemented in terms of file descriptors, you can |
| extract the file descriptor from a stream and perform low-level |
| operations directly on the file descriptor. You can also initially open |
| a connection as a file descriptor and then make a stream associated with |
| that file descriptor. |
| |
| <p>In general, you should stick with using streams rather than file |
| descriptors, unless there is some specific operation you want to do that |
| can only be done on a file descriptor. If you are a beginning |
| programmer and aren't sure what functions to use, we suggest that you |
| concentrate on the formatted input functions (see <a href="Formatted-Input.html#Formatted-Input">Formatted Input</a>) |
| and formatted output functions (see <a href="Formatted-Output.html#Formatted-Output">Formatted Output</a>). |
| |
| <p>If you are concerned about portability of your programs to systems other |
| than GNU, you should also be aware that file descriptors are not as |
| portable as streams. You can expect any system running ISO C<!-- /@w --> to |
| support streams, but non-GNU systems may not support file descriptors at |
| all, or may only implement a subset of the GNU functions that operate on |
| file descriptors. Most of the file descriptor functions in the GNU |
| library are included in the POSIX.1 standard, however. |
| |
| </body></html> |
| |