[C / C ]get() and fget() in C/C – Warning when user gets() – Warning when use gets()

Normally we use a variable to enter the function scanf(), This function is used to enter 1 string but when met with blank sign (spaces, enter,…) then do not read anymore and that string was cut from, Example entry “nguyen van agency” then we only get “nguyen”.
The fix here is that we use the function gets() to enter the string. 😀 Khi đó ta sẽ nhận được chuỗi nguyên vẹn.
However arose a tricky thing is that if the buffer (stdin file) which contains the characters have not been read (VD blanks by scanf() and still could not read the file stdin) it gets() right to receive the letters and did not enter the string (The phenomenon of floating command). And our treatment before use gets() the addition of 1 stdin command to delete the night that fflush(stdin). Then we were able to enter the string 1 stable

 
#include <stdio.h>
  
int main()
{
    int age;
    char name [256];
    printf ("Insert your age: ");
    scanf("%d",&age);
    fflush(stdin);
    printf ("Insert your name: ");
    gets (name);
    printf ("Your name is: %s and you are %d years oldn",name, age);
    return 0;
}

But the problem again arises in the windows we will not see 1 warnings in code on, velvet in the code on Linux though no errors but arise 2 problem. Firstly, the machine will compile warning ” warning: ‘char* gets(char *)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] ” and the second is the command fflush(stdin) will not work, I drifted back orders.

First, we go handle this alert. In the book “Secure Programming Cookbook for C and C ” there is a passage in chapter 3.1 having said that: gets() is a function located in the old library of C. This function is used to enter the chain but very comfortable, but there is a very dangerous point is not checked buffer size of the input data. That will make the program crash or worse if the whole system crash if input exceeds the declaration or processing. And especially on linux, the memory management very closely (many more windows !) so the warning is completely accurate, no new warning is problematic !
You can try by compiling your program in windows. In most cases (use many different compilers) no warning. But on the other linux, because it is the only mechanism to ensure the stability of the system, ensure that the system is not being eaten by a stray bullet programmers , otherwise GCC compiler is a free but very modern but, it always fixes and updates of the latest standard C / C
(Commentary drnoxxx in ddth.com)
So how to handle it? One solution for us is to use fgets() to enter. fgets() is a function that allows us to import from file and specify size when entering chain. Code illustrations used to import file

#include <stdio.h>

int main()
{
	FILE * pFile;
	char mystring [100];
	pFile = fopen ("myfile.txt" , "r");
	if (pFile == NULL) perror ("Error opening file");
	else
	{
		if ( fgets (mystring , 100 , pFile) != NULL ) //nhap chuoi toi da 100 phan tu tu file
		puts (mystring);
		fclose (pFile);
   }
   return 0;
}

But if you want to use fgets dunu() entered from the keyboard, the stars? is simple, you do not need to declare the file as above, just replace the file on stdin is okay by !

fgets (mystring , 100 , stdin);

Solving the first problem 2 buffer is deleted if not used fflush(stdin) ? The answer is simple just read all the spaces from stdin 1 certain variables and continued his work is okay.
But another problem arises when using fgets() entered from the keyboard when you have finished typing the string must press enter and it will always read your enter key chain, when the cursor is automatically newline, and this becomes difficult when we want to write 2 xuau same 1 current. Solving this problem we used another technique that adds a NULL is assigned to the last character of bananas enter the command

string[strlen(string)-1] = '&#092;&#048;'; //chu ý khai báo string.h

Here is the code used fgets().

#include <stdio.h>
#include <string.h>
int main()
{
	int age;
	char name [256];
	printf ("Insert your age: ");
	scanf("%d",&age);
	if (fgets (name,256,stdin) != NULL); //xoa bo dem
	//fflush(stdin); //khong co tac dung xoa bo dem
	printf ("Insert your name: ");
	if (fgets (name,256,stdin) != NULL); //nhap ten
	name[strlen(name)-1] = '&#092;&#048;';
	printf ("Your name is: %s and you are %d years oldn",name, age);
	return 0;
}