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