[Second edition of advanced programming in UNIX environment] Chapter 5 of Reading Notes

Source: Internet
Author: User

Standard I/O 5.1 was written by Dennis Ritchie around 1975.
Chapter 3 I/O is for file descriptors. The standard I/O Library is centered around stream. When opening or creating a file, we have associated a stream with the file.
Stream ing determines whether the read/write characters are single or multi-byte characters. I/O functions can be used to change the orientation of an unoriented stream. Only two functions can change the stream orientation. Freopen is used to clear stream targeting.
Int fwide (File * FP, int mode); the negative number of mode attempts to specify the byte orientation. To specify the width orientation. 0. If this parameter is not set, the current targeted value is returned.
5.3 The file descriptors stdin_fileno, stdout_fileno, and stderr_fileno are the same as the files referenced by stdin, stdout, and stderr.
In the former # include <unistd. h>, the suffix is defined in # include <stdio. h>.
5.4 The purpose of the standard I/O buffer is to reduce the number of read/write calls.
1) Full buffer. The actual I/O operation is performed only when the standard I/O buffer is filled. Files on disks are common. You can call fflush (write) and tcflush (discard) the existing buffer content.
2) Row buffering. When a line break is encountered in the input and output, the standard I/O Library performs the I/O operation. A> fixed length, I/O operation when full. B> If the standard I/O Library requires that the input data be obtained from a stream with no buffer or line buffer, the operation is executed.
3) No buffer. The standard I/O Library does not buffer characters. For example, fputs function. Standard stderr is generally not buffered.
Iso c requires the following buffer features:
# The full buffer is used only when stdin and stdout do not involve interactive device.
# Stderr is not a full buffer.
Many default systems:
# Stderr is not buffered.
# Other streams related to the terminal device are line buffers. Otherwise, it is a full buffer.

Void setbuf (File * restrict FP, char * restrict BUF); enable or disable the buffer mechanism.
Int setvbuf (File * restrict FP, char * restrict Buf, int mode, size_t size );
Int fflush (File * FP); force flush a steam to transfer all data not written to the kernel. If FP is null, all output streams are flush.
Details

5.5 open a standard stream.
File * fopen (const char * restrict pathname, const char * restrict type); open the specified stream.
File * freopen (const char * restrict pathname, const char * restrict type, file * restrict FP );
Open the specified file in the specified stream. If the stream has been opened, close it first. If yes, clear the direction. It is generally used to open a specified file as stdin, stdout, or stderr.
File * fdopen (INT filedes, const char * type); combines the standard I/O stream with the file descriptor. Filedes is usually the return value of the function for creating "Pipeline" and "network communication channel. (Posix.1 private)
For the open mode, see

Int fclose (File * FP); close the open stream. The output data in the buffer is flushed before being disabled. Discard any input data.
5.6 once a stream is opened, you can select among the three different types of unformatted I/O and read and write the stream.
1) one character I/O at a time.
Input: int GETC (File * FP );
Int fgetc (File * FP );
Int getchar (void); equivalent to GETC (stdin ).
Whether it is an error or EOF, the three functions return the same value to distinguish. For example:
Int ferror (File * FP );
Int feof (File * FP );
Void clearerr (File * FP); in most implementations, each stream maintains a in the file object) Error mark. B) EOF flag. This function can clear the two flags.
Int ungetc (int c, file * FP); after reading data from the stream, you can call this function to compress the characters and send them back.
Output: int putc (int c, file * FP );
Int fputc (int c, file * FP );
Int putchar (int c); equivalent to putc (C, stdout ).
2) I/O of each row. Fgets, fputs
3) Direct I/O. Fread, fwrite.
5.7 I/O for each row.
Input: char * fgets (char * restrict Buf, int N, file * restrict FP); read n-1 characters from the stream.
Char * gets (char * BUF); read from stdin, do not talk about line breaks into the buffer, not recommended.
Output: int fputs (const char * restrict STR, file * restrict FP); write the string ending with null to the specified stream. Null at the end is not written. A row may not be output every time. If it is null, it is stopped.
Int puts (const char * Str); writes the end of null to stdout. null is not written. However, puts writes a linefeed to stdout. It is not recommended.
5.8 The standard I/O library is not much slower than calling the Read and Write functions directly.
5.9 size_t fread (void * restrict PTR, size_t size, size_t nobj, file * restrict FP );
Size_t fwrite (const void * restrict PTR, size_t size, size_t nobj, file * restrict FP );
Common usage: 1) read or write a binary array. For example:
Float data [10]; fwrite (& Data [2], sizeof (float), 4, FP); write 2-5 elements to the file.
2) read or write a structure.
5.10 There are three methods to locate a stream.
1) ftell and fseek. It is assumed that the file location can be stored in a long.
2) ftello and fseeko. Single UNIX specification introduced. Use off_t instead of long.
3) fgetpos and fsetpos. Introduced by Iso c. Use the abstract data type fpos_t to record the file location.
5.11 format the output
Printf, fprintf, sprintf, snprintf.
Vprintf, vfprintf, vsprintf, and vsnprintf. variable parameters.
Format input
Scanf, fscanf, sscanf.
5.12 for stream, you can use int fileno (File * FP); to obtain its file descriptor.
FP-> _ io_file_flags & _ io_unbuffered. If there is a buffer, the size is FP-> _ io_buf_end-FP-> _ io_buf_base;
5.13 create a temporary file.
Char * tmpnam (char * PTR); Maximum number of calls tmp_max.
File * tmpfile (void); create a temporary binary file (type WB +), close the file or automatically delete the file after the program ends.
Char * tempnam (const char * directory, const char * prefix); xsi extension, non-Iso c.
Int mkstemp (char * template); xsi extension, non-Iso c.
5.14 standard I/O efficiency is not high. SFIO (Korn & VO [1991]) can be used as an alternative. Krieger, stunmm & unrau [1992] use the ing file-MMAP function.
Many standard I/O Library implementations can be used in C function libraries. Two implementations are the uclibc C library and newlibc library.
5.15 most UNIX applications use the standard I/O library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.