Analysis of standard I/O Library

Source: Internet
Author: User

This article mainly introduces C language standard I/O library, this is let me puzzled for a long time a knowledge point, I believe for many C language beginners, the concept of I/O is not very clear, I would like to briefly introduce today. This article refers to advanced programming for the UNIX environment.

First look at a program:

#include <stdio.h>int main (void) {    printf ("hello,world!\n");    return 0;}

This is probably the first program written by anyone learning C, the main use of this program is I/O, where the header file is the standardI/O library files, the function of the printf function is to output the characters in quotation marks to the standard output stream, usually refers to the program's control terminal, or display on the screen. Then, if you want to output to a file, you can use fprintf, which can write characters to the specified stream. So what is the concept of flow? Typically, a stream is a struct named file that contains all the information that the standard I/O library needs to manage the flow, including: file descriptors for actual I/O, pointers to the stream buffers, length of buffers, and so on . Here's the question, what is the file descriptor? What is the concept of buffers? What the hell is this flow?


I think I should start with the file I/O.

File I/O, as the name implies, reads data from a file and writes the data to such an interface as a file. In Unix systems, frequently used file I/O functions are: Open,read,write,lseek and close, which function to open files, read files, write files, locate files, and close a file. So how do I get a specified file? This requires the use of file descriptors. The file descriptor is a non-negative integer, such as the standard input file descriptor may be 0, the standard output file descriptor may be 1, the standard error is 2, such as a file hello.c file descriptor is 5, and so on. As an example:

#include <fcntl.h> #include <unistd.h> #include <stdio.h> #define Buffsize 4096int Main (void) {    int n;    Char buf[buffsize];        while ((n = read (stdin_fileno,buf,buffsize)) > 0)        if (write (stdout_fileno,buf,n)! = N)            printf ("Write error \ n ");    if (n < 0)        printf ("Read error\n");    Exit (0);}
The purpose of this program is to copy the contents of the standard input to the standard output.

The following is a simple analysis of the program: header file declaration needs to use the function, defined a size of 4096 Buffsize,stdin_fileno represents the standard input stream file descriptor, BUF variable storage read content,buffsize is size, every time you read so much data, it is possible to not read so much data until the end of the read or error. The meaning of this sentence is to read the Buffsize data from the standard input , and the return value is the number of data actually read in. The Write function writes the contents of the BUF to the standard output, which is Stdout_fileno, the number is N, which is the last read. This is the main body of the program, when we need to read from other files or write to other files (instead of the callout input and output), we just need to replace Stdin_fileno and Stdout_fileno with the file descriptor we specified. This file descriptor can be obtained by using the Open function.

This program has a key point, is the size of the buffsize, why is 4096?

The authors of advanced programming for the UNIX environment have tested various sizes:


We see that when buffsize is 1 o'clock, it takes the longest time because the kernel needs to read and write the files the most, and when the buffsize becomes larger, the number of reads and writes becomes less, and then the time is short. The size of this buffsize is the size of the buffer, which we define as buf[buffsize], which is the buffer. Its purpose is to let the kernel read and write file data, it is enough to buffsize data read and write, rather than a byte unit to read or write, this can improve efficiency.


So what is the standard I/O library? The standard I/O library gives a proper default buffer instead of having to set it ourselves. Of course, you can modify it. The concept of flow also understands that a stream is associated with a particular file, including information such as file descriptors, buffer pointers, and buffer sizes. The final I/O operation is done through file I/O (I guess), but standard I/O has already set you a default more appropriate buffer to improve read and write efficiency.

Analysis of 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.