Three types of buffereing provided:
- Fully buffered. Files residing on disk are normally fully buffered by the standard I/O library.
- Line buffered. Line buffering is typically used on a system when it refers to a terminal: standard input and standard output.
- Unbuffered. It makes the data output as soon as possible. The standard error stream, for example, is normally unbuffered. This is so that any error messages are displayed as quickly as possible. This function must call after the stream has been opened but before any other operation is performed on the stream.
- With setbuf, we can turn buffering on or off. To enable buffering, buf must point to a buffer of length BUFSIZ. To disable buffering, we set buf to NULL.
- With setvbuf, We specify exactuly which type of buffering we want. This is done with the mode argument: _IOFBUF -> fully buffered, _IOLBUF -> line buffered, _IONBUF -> unbuffered. The size argument equal the length of the buf.
- In general, we should let the system choose the bufer size and automatically allocate the buffer. When we do this, the standard I/O library automatically release the buffer when we close the stream. This function causes any unwritten data for stream to be passed to the kernel. As a special, if the fp is NULL, this function causes alll output streams to be flushed.
2. Opening Steam
- The freopen function opens a specified file on a specified steam, closeing the stream first if it is already open. We usually use it link a standard I/O to a file as freopen("filename","r",stdin) this function equal dup2(fd,0).
- The fdopen function takes an existing file descriptor, which we could obtain form the open, dup, dup2, fcntl, pipe, socket, socketpair or accept functions, and associates a standard I/O steam with the descriptor. The descriptor has already been opened, so opening for write does not truncate the file. Also, The standard I/O append mode cannot create the file.
When a file is opened for reading or writing, the following restrictions apply:
- Output cannot be directly followed by input without an interveing fflush, fseek, fsetops or rewind.
- Input cannot be directly followed by output without an interveing fseek, fsetpos or rewind, or an input opereation that encounters and end of file.
- Note that if a new file is created by specifying a type of either w or a, we are not able to specify the file's access permission bits, as we were able to de witch the open function and the create function.
3. Reading and Writing a Stream
Once we open a stream, we can choose form among three types of unformatted I/O:
- Character-at-a-time I/O The defferents bewteen getc and fgets is that getc can be a marco, whereas fgets cannot be implemented as a macro. But the detail is depond on your system, in my system(Fedora 7) getc is a macro, so in my system getc function is faster than fgetc function.
- Line-at-a-time I/O We'd better don't use gets and puts function beacuse these my lead your program overflow.
- Direct I/O These functions have to common uses: read or write a binary arrary, read or write a structure.
4. Implementation Details
Each standard I/O steam has a associated file descriptor, and we can obtain the descriptor for a stream by calling fileno.We need this function if we want to call the dup or fcntl functions, for example.