Enter the putc in the C standard library (4) -- & quot; stdio. h & quot;

Source: Internet
Author: User

It took some time to make the garden look good. continue now.

Function Name:Putc 
Function: outputs one character to a specified stream.
Usage: intPutc(Int ch, FILE * stream );

 _putc_lk(_c,_stream)    (--(_stream)->_cnt >= 0 ? 0xff & (*(_stream)->_ptr++ = (char)(_c)) :  _flsbuf((_c),(_stream)))

See if this macro is familiar, like the getc macro.

Therefore, it is easy to have a problem. It is also to change the information in the IO control block. Can putc and getc be used for read/write operations normally?

The following is a simple example:

 #include <stdio.h>   main(          i =     FILE *fp = fopen(,    a =    putc(    putc(    putc(    putc(       }

The above Code cannot normally write content in the input.txt file.

Through one-step debugging, we can see that getc is successful, and putc also changes the buffer zone, but there is a problem in writing files.

Furthermore, it compares the differences between getc and putc and fp values after fopen. As you can see, when getc is performed first, fp-> _ flag = 137, while when putc is performed first, fp-> _ flag = 138. Obviously, when the function is used to read and write fp for the first time, different read and write tags will be set for fp. This flag will block other types of operations.

Therefore, putc is successful after the code fp-> _ flag = 8th is added to the Code in line 2.

In the sense of the buffer zone, it is not said that read and write operations are not allowed at the same time. The reason may be that reading and writing files at the same time may cause confusion of reading information or writing information, or may be the cause of implementation. Guess.

Back to the truth:

Therefore, putc takes effect only when writing information in FILE.

When stream-> _ cnt is greater than 0 (there is still a write vacancy in the buffer zone), write continues. If there is no vacancy, call _ flsbuf (_ c), (_ stream )), this function is used to call WriteFile, write the content in the buffer to the file, and reset the position of _ ptr and the size of _ cnt.

Simple logic.

 

End

 

 

 

 

 

 

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.