A few details about fputs and fgets.

Source: Internet
Author: User

The two standard IO fputs and fgets in the C language are read on the line for the data! Here about these two IO functions I have a few small details I would like to share with you here, I hope to be able to help you!

First, the function declarations of these two functions are affixed, and the following are discussed based on the two function declarations:

  

  

The code I use for debugging is as follows:

1 /*The input of this program is NIHAOA, then through GDB debugging to view the contents of the Fputs buffer area2  */3#include <stdio.h>4#include <string.h>5#include <stdlib.h>6#include <errno.h>7 8 #defineMAXLINE 49 Ten intMainintargcChar*argv[]) One { A     CharBuffer[maxline]; -memset (Buffer,1, MAXLINE); -     CharBuffer_o[bufsiz]; thememset (Buffer_o,1, bufsiz); -  - setbuf (stdout,buffer_o); -  +      while(NULL! =fgets (Buffer,maxline,stdin)) -     { +     if(EOF = =fputs (buffer,stdout)) A     { atprintf"[fputs]:%s", Strerror (errno)); - exit (exit_failure); -     } -     } -  -     if(Ferror (stdin))//Check if the loop stop above is due to an error in     { -printf"[fgets]:%s", Strerror (errno)); to exit (exit_failure); +     } -  thebuffer_o[6] ='k';  *buffer_o[7] ='J'; $     /*Here I adjusted the contents of the fputs buffer, and the last newline character became K, the latter of the newline character became J, but the Fputs lostPanax Notoginseng * out of time or output to the content of K, the back of the J did not output. So the fputs output is not based on the last ' s ' - * Fixed, but in this program has a counter, to measure the total number of characters entered, and then output.  the      */ +  A     return 0; the}

First, the first question, fgets the length of the data that is read from its buffer every time is SIZE-1 bytes, and then it automatically adds a '% ' symbol at the end of the string! When fgets the string into its buffer, it will automatically ignore the end of the ' + ' symbol! As shown in the following:

  

Like the program above! The character array buffer is used to act as the buffers for this program! And that buffer_o me through the SETBUF function to make it into the standard output buffer! For the sake of distinction, I set the data initialization of both arrays to 1.

For example, I set two breakpoints on the 19 lines and 21 lines of the program above! Then run to view buffer memory!

The first is line 19th, at which time the memory of buffer is all 1:

  

Then run a sentence, I entered the data is Nihaoa<cr>, because the contents of buffer is not large enough, all it will only read Size-1 is 3 bytes of content, the last byte is filled with 0, as shown in:

  

The above illustrates the content of fgets, which reads only size-1 bytes from its buffer, and then appends a 0 to the end of the string;

Next we proceed to debug, continue to run down one step, the result is as follows:

  

Here the buffer_o is a buffer of stdout, at which time there is only 3 bytes of content, which explains the part about the fputs, reads the string from the target memory, and ignores the '-s ' at the tail of the string.

  

Then we can say the second problem, that is, the fputs program should have a counter, to count the stdout buffer in a total number of characters, fputs output is based on this counter to output! Let's take the above code as an example, and this time we set a breakpoint on lines 34 and 40. Look at the contents of Buffer_o in memory! As shown in the following:

  

This time my input is still nihaoa<cr>, this back stdout buffer in the content is nihaoa<cr> ASCII code! Then I changed the next ASCII code for the carriage return and the carriage return as shown:

  

The carriage return character becomes K, and its next becomes J. Then we'll look at the results of the output!

Since GDB automatically added a line break, I ran it in a normal way! As shown in the following:

  

The last input is Nihaoak, and there is no more, here is to show that the output of Fgets is based on its counter!

A few details about fputs and fgets.

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.