Standard I/O Library

Source: Internet
Author: User
Tags sprintf
Standard I/O is defined by the ANSI C standard and can be used not only on Unix/Linux, but also elsewhere.
Operations on files corresponding to the standard I/O library are performed around streams. When a file is opened or created using the standard I/O library, a stream is associated with a file. The functions of the standard I/O Library describe files based on the file object pointer. This object is a struct that contains all the information required by the I/O library to manage the stream: file descriptor used for actual I/O, pointer to the stream cache, cache length, number of characters in the cache, error mark, and so on.

Standard input, standard output, standard error
Similar to file descriptors stdin_fileno, stdout_fileno, and stderr_fileno, standard I/O also have these three types, which are referenced by predefined pointers stdin, stdout, and stderr. These three pointers are defined in the header file <stdio. h>.

Buffer
As we all know, I/O operations are time-consuming. If only a small number of reads or writes are performed for each I/O operation, the number of I/O operations will certainly increase. The buffering function is to first store the data for I/O operations, and then perform the actual I/O operations when a certain amount of data is saved. The system calls read and write without buffering, So I/O operations are performed each time. Standard I/O is buffered, which automatically manages the buffer. Standard I/O provides the following three buffers:
1. Full Buffer: indicates that the actual I/O operations are performed only after the I/O buffer is filled. Files on disks are usually fully buffered by standard I/O. When I/O operations are performed for the first time on a stream, the related standard I/O functions usually call the buffer zone opened by malloc.
2. Row Buffer: when the input or output encounters a line break, the standard I/O Library performs the actual I/O operation. When a stream involves terminals (such as standard input and standard output), row buffering is usually used.
3. No buffer: The standard I/O Library does not buffer characters and directly calls the system call. Standard Error stderr is usually not buffered, so that the error information can be displayed as soon as possible.
The concept of flushing refers to clearing the buffer zone and performing I/O operations on the buffer zone data. Call the fflush function to fl a stream.
You can use the function to change the buffer type:
# Include <stdio. h>
Void setbuf (File * restrict FP, char * restrict BUF );
Int setvbuf (File * restrict FP, char * restrict Buf, int mod, size_t size );
Rinse stream usage:
# Include <stdio. h>
Int fflush (File * FP );
If FP is null, all output streams are flushed.

Open stream
# Include <stdio. h>
File * fopen (const char * restrict pathname, const char * restrict type );
File * fopen (const CAHR * restrict pathname, const char * restrict type, file * restrict FP );
File * fdopen (INT filedes, const char * type );
Close stream
# Include <stdio. h>
Int fclose (File * FP );


Read/write stream
After the stream is opened, you can select from three different types of unformatted I/O (and formatted I/O) to read and write the stream:
1. I/O of each character. One character is read or written each time. If the stream is buffered, standard I/O processes all the buffers.
2. I/O of each row. If you want to read or write a row at a time, use fgets or fputs to terminate the operation with a linefeed.
3. Direct I/O. Fread and fwrite support this type of I/O. Each I/O operation reads or writes a certain number of objects with a specified length. These two functions are often used to read or write a structure from a binary file each time.


I/O that reads one character each time(Function end C indicates one character each time, char)
# Include <stdio. h>
Int GETC (File * FP );
Int fgetc (File * FP );
Int getchar (void); // equivalent to GETC (stdin)
Each input function has an output function.
# Include <stdio. h>
Int putc (int c, file * FP );
Int fputc (int c, file * FP );
Int putchar (int c); // equivalent to putchar (C, stdout );


Each line I/O(Function end s indicates each row, string)
# Include <stdio. h>
Char * fgets (char * restrict Buf, int N, file * restrict FP );
Char * gets (char * BUF); // read from standard input
Each input function has an output function.
# Include <stdio. h>
Int fputs (const char * restrict STR, file * restrict FP );
Int puts (const char * Str );


Binary I/O
Binary I/O is often used to operate a structure at a time. If GETC or puts is used to read and write a structure, it takes a lot of time to process a byte in a loop. If fputs or fgets is used, fputs/fgets will stop when it encounters NULL bytes, and the structure may contain null bytes, which cannot meet the read structure requirements. Therefore, two functions are provided to execute binary I/O.
# Include <stdio. h>
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 );
The basic problem with binary I/O is that it can only be used to operate on the same file system. With the rise of Network File System systems, the use of these two functions is limited.


Format I/O
Format the input four functions
# Include <stdio. h>
Int printf (const char * restrict format ,......);
Int fprintf (File * restrict FP, const char * restrict format ,......);
Int sprintf (char * restrict Buf, const char * restrict format ,......);
Int sprintf (char * restrict Buf, size_t N, const char * restrict format ,......);
Format input
# Include <stdio. h>
Int scanf (const char * restrict format ,......);
Int fscanf (File * restrict FP, const char * restrict format ,......);
Int sscanf (const char * restrict Buf, const char * restrict format ,......);


In Unix systems, the standard I/O Library eventually calls the system of file I/O. Each standard I/O Stream has a file descriptor associated with it. You can use the following function to obtain the corresponding file descriptor.
# Incude <stdio. h>

Int fileno (File * FP );

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.