The difference between gets (), puts () and fgets (), fputs ().

Source: Internet
Author: User
Tags stdin
/*
#include <stdio.h>
#include <stdlib.h>


#define MAX 81


int main (void)
{
Char Name[max];
Char *ptr;


printf ("Please enter name: \ n");
ptr = fgets (name, MAX, stdin);
printf ("%s"?) ah! %s\n ", name, PTR);

GetChar ();
return 0;
}
*/
/*********************************************************************************
Please enter your name:
Qinshihuang
Qinshihuang


? ah! Qinshihuang




A problem has arisen: the supposed question mark is on the line, but is afraid to run to the next line.
The problem is that fgets () stores newline characters in a string so that the line breaks are displayed each time the characters are displayed.
We can use STRCHR () to locate and delete line breaks.
***********************************************************************************/


/*---------------------------------------------------------------------------------


Gets () is convenient for interactive programs. Because there is no predetermined length of the string, so gets () does not know when
End. The workaround is to read the string until a newline character (\ n) is encountered and press ENTER to produce this character. He
Reads all characters before the line break (excluding line breaks), after which a null character is added &LT;\0&GT;
A string to the program that invoked it. It reads the newline character and discards it, so the next read starts on a new line.
Gets () returns a pointer to the first address of the string.


Because gets () does not check whether the destination array can hold input, it is unsafe. Yes, someone noticed it a few years ago.
The code in the Nuix operating system uses gets (), so they exploit this weakness to overwrite the operating system code with a long input
, thereby inventing "worm" that spreads on UNIX networks.


Fgets () and gets () are different in three ways: * * *


1.fgets () requires a second parameter to describe the number of characters read. If this argument is N, fgets () reads up to n-1 characters
Or read a line break, which ends the input by the first one that satisfies it.


2. If fgets () reads a newline character, it is stored in a string rather than discarded as gets ().


3. He also needs a third parameter to specify which file to read. When reading data from the keyboard, you can use STDIN (standard input)
As a parameter, this identifier is defined in stdio.h.






scanf () and gets () * * *


The main difference is how they decide the end of the string.


SCANF () is based on getting words. Gets () is based on the get string.
scanf () makes two methods of decision book input end:
1. If you use the%s format, you'll end up with a blank character,
2. If the length of the specified%10s, then read the end of the 10th.
The two first meet each end of the input.

Gets () end input on one:
A line break was encountered.


Gets () is more suitable for text input. SCANF () is applied to variable input with attributes (that is, a control character).


printf () and puts () * * *


After the puts () output, a newline character is automatically added thereafter.


Fputs () and puts ()

Fputs () requires a second parameter to describe the file to write. Use stdout for output display.
Gets () discards the newline character in the input, puts () adds a newline character to the output;
Fgets () stores line breaks in the input, fputs () does not add line breaks for the output.




Suppose you write a loop, read a row, and echo it back to the next line.


Char line[80];
while (gets)
Puts (line);




Char line[80];
while (fgets)
Fputs (line);




The first loop puts () adds a newline character to him, so the characters in each array are displayed on a separate line.
In the second loop, the Fgets () stores a newline character, so the two outputs have the same effect.



-------------------------------------------------------------------------------------*/

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.