Standard I/O library for Linux/UNIX

Source: Internet
Author: User
When a stream is opened in the standard IO Library, the standard IO function fopen returns a pointer to the FILE object. This object is usually a structure that contains the standard I standard IO Library

When a stream is opened, the standard I/O function fopen returns a pointer to the FILE object. This object is usually a structure that contains all the information required by the standard I/O library to manage the stream, including: the actual I/O file descriptor, pointer to the buffer, length of the buffer, number of characters in the buffer zone, and error mark. To reference a stream, you must pass the FILE pointer as a parameter to each standard I/O function.

For standard input, standard output, and standard errors, their file descriptors correspond to STFIN_FILENO, STDOUT_FILENO, and STDERR_FILENO. These three standards are referenced by stdin, stdout, and stderr. The three file pointers and standard I/O functions are defined in the header file. .

Buffer

The standard I/O library provides buffer to minimize the number of read and write calls. Three types of buffering are provided:

1) Full Buffer: the actual I/O operation is performed only after the standard I/O buffer is filled.

2) row Buffering: when a line break is encountered in the input and output, the standard I/O library performs the I/O operation.

3) no buffer: the standard I/O library does not store character buffering.

Generally, standard errors are not buffered. the stream that opens the terminal device is buffered by a row, and all other streams are fully buffered. When the stream is fully buffered, but the buffer is partially filled, the fflush function can be used for flushing.

You can call the following function to change the buffer type:

# Include

Void setbuf (FILE * stream, char * buf );

Int setvbuf (FILE * stream, char * buf, intmode, size_t size );

At any time, we can forcibly fl a stream:

# Include

Int fflush (FILE * stream );

This function transfers all unwritten data of the stream to the kernel. As a special case, if fp is NULL, this function will cause all output streams to be flushed.

Open Stream

# Include

FILE * fopen (const char * path, const char * mode );

FILE * fdopen (int fd, const char * mode );

FILE * freopen (const char * path, const char * mode, FILE * stream );

The differences between the three functions are:

1) fopen open a specified file.

2) fropen opens a specified file on a specified object. if the stream has already been opened, close the stream first. If the stream has been targeted, fopen clears the targeting. This function is generally used to open a specified file as a predefined stream: standard input, standard output, or standard error.

3) fdopen obtains an existing file descriptor and combines a standard I/O stream with this descriptor. This function is often used by the descriptor returned by the create pipeline and network communication function. Because these special types of files cannot be opened using the standard I/Ofopen function, we must first call the dedicated function of the device to obtain a file descriptor, then, use fopen to associate a standard I/O with the descriptor.

The mode parameter can be set to the following 15 different values:
R or rb: open for Read

W or wb: truncates the file to 0 long, or creates a file for writing.

A or AB: add; open in file write or in Write

R + or r + B or rb +: open for read and write

W + or w + B or wb +: truncates the file to 0, or opens the file for reading and writing.

A + or a + B or AB +: open or create a file for reading and writing at the end of the file

# Include

Int fclose (FILE * fp );

Rinse the output data in the buffer before the file is closed. If the standard I/O library has automatically allocated a buffer for the stream, the buffer is released.

Read and write streams

Once a stream is opened, you can select among three different types of unformatted I/O to read and write the stream:

1) each character is I/O. Read or write one character at a time. if the stream contains a buffer, the standard I/O function processes all the buffers.

2) I/O of each row. If you want to read or write a row at a time, use fgets and fputs. Each line ends with a linefeed. When fgets is called, the maximum capacity that can be processed should be stated.

3) direct I/O. Fread and fwrite functions support this type of I/O.

One character each time I/O

Input function:

# Include

Int getc (FILE * stream );

Int fgetc (FILE * stream );

Int getchar (void );

Getchar () is equivalent to getc (stdin ). The difference between getc and fgetc is that getc can be implemented as a macro, while fgetc cannot be implemented as a macro.

Whether an error occurs or the end of the file, the three functions return the same value. To distinguish between errors and reaching the end of a file, the ferror and feof functions must be called.

# Include

Int feof (FILE * stream );

Int ferror (FILE * stream );

Return values of these two functions: if the condition is true, a non-0 value is returned; otherwise, 0 is returned.

Each stream maintains two marks in the FILE object: the error mark and the end mark of the FILE.

Call clearerr to clear the two flags.

Void clearerr (FILE * stream );

After reading data from the stream, you can call ungetc to push the character back.

Int ungetc (int c, FILE * stream );

After the characters are pushed into the reflux, they can be read from the stream again, but the character order is the opposite of the Order of the characters to be read back.

For output functions:

# Include

Int putc (int c, FILE * stream );

Int fputc (int c, FILE * stream );

Int putchar (int c );

Putchar (c) is equivalent to putc (c, stdout ). Putc can be implemented as a macro.

Each line I/O

# Include

Char * fgets (char * s, int size, FILE * stream );

Char * gets (char * s );

Fgets reads data from a specified stream. the buffer length must be specified. This function keeps reading the next line break, but cannot exceed n-1 characters. The characters read are sent to the buffer zone. The buffer ends with null characters. If the number of characters in the modified line (including the last line break) exceeds n-1, fgets returns only one incomplete line, but the buffer always ends with null characters. The next call to fgets will continue to read and modify rows.

Gets reads data from standard input. It is an unrecommended function, because the length of the buffer cannot be specified, which may cause buffer overflow and write it to the bucket after the buffer, causing unpredictable consequences.

Fputs and puts provide the ability to output a row each time.

Int fputs (const char * s, FILE * stream );

Int puts (const char * s );

Binary I/O

# Include

Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream );

Size_t fwrite (const void * ptr, size_tsize, size_t nmemb, FILE * stream );

The above two functions can read or write the entire structure at a time.

Locate stream

There are three methods to locate standard I/O streams

1) ftell and fseek. These two functions require that the file location be stored in a long integer.

2) ftello and fseeko. They may make the file offset not necessarily use long shaping. They use the off_t data type instead of the long integer.

3) fgetpos and fsetpos. They use the abstract data type fpos_t to record the file location. This type of data can be defined as the length required to record the location of a file.

# Include

Int fseek (FILE * stream, long offset, intwhence );

Long ftell (FILE * stream );

Int fseeko (FILE * stream, off_t offset, intwhence );

Off_t ftello (FILE * stream );

Int fgetpos (FILE * stream, fpos_t * pos );

Int fsetpos (FILE * stream, fpos_t * pos );

Format I/O

Output:

# Include

Int printf (const char * format ,...);

Int fprintf (FILE * stream, const char * format ,...);

Int sprintf (char * str, const char * format ,...);

Int snprintf (char * str, size_t size, constchar * format ,...);

Input:

# Include

Int scanf (const char * format ,...);

Int fscanf (FILE * stream, const char * format ,...);

Int sscanf (const char * str, const char * format ,...);

Fileno function

The standard I/O library eventually calls the I/O system call function. Each standard I/O stream has a file descriptor associated with it. you can call the fileno function on a stream to obtain its descriptor.

Int fileno (FILE * stream );

Temporary files

# Include

Char * tmpnam (char * s );

FILE * tmpfile (void );

The tmpnam function generates a valid path name string different from the existing file name. Each time you call it, it generates a different path name and can call TMP_MAX at most.

Tmpfile creates a temporary binary file.

There are also two similar functions:

# Include

Char * tempnam (const char * dir, const char * pfx );

Int mkstemp (char * template );

Related Article

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.