[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] = '\0'; //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] = '\0'; printf ("Your name is: %s and you are %d years oldn",name, age); return 0; }
'\0’ ý nghĩa là gì vậỵ bạn.
Là ký tự kết thúc chuỗi đó bạn
Friend, cho mình hỏi tình huống này với. Nếu file đầu vào của mình trên một dòng có các giá trị và cách nhau một khoảng trắng. Ví dụ như: 2 3 4 6 trên cùng một dòng và mình muốn đọc các giá trị 2, 3, 4, 6 on and recorded in an array, then how to remove white space there, dear
Thank you very much
You just use fscanf is that.
my friend, tại sao câu lệnh if (fgets (name,256,stdin) != NULL); lại có tác dụng xóa bộ nhớ đệm
Vì nó đọc chuỗi trong bộ nhớ đệm mà 🙂
sao e ghi vào ‘\0’ máy báo lỗ ạ
A được rồi a ơi
loi gi the e.
anh ơi sao em chạy chương trình trên của anh nó thông báo như này
1.c: In function ‘main’:
1.c:13:28: warning: character constant too long for its type [enabled by default]
name[strlen(name)-1] = ‘\0';
^
1.c:13:5: warning: overflow in implicit constant conversion [-Woverflow]
name[strlen(name)-1] = ‘\0';
^
Nó chỉ là cảnh báo thôi. It's okay.
Còn get_s như thế nào so với fget hỏi anh?
get_s nó giống gets, chẳng qua là do từng phiên bản thôi. fget is read from the file nhé. http://en.cppreference.com/w/c/io/gets
Trong cú pháp fgets ( tank *, int , FILE *) , but it is limited in character int. should always have to enter a larger number string should read.
Example: 1234that one, must be to place greater int 7. if the 2 or 3 then the reading is missing.
Teacher does not know how to enter numbers into the int and still run right…!!
Just enter 255 is. It defined function arguments that must be included in severance.
Evening ..!!
^^
let me ask why it reported
name[strlen(name)-1] = ‘\0';; was
character constant too long for its type
You see the characters' 0’ was true that no apostrophe?
Waiter, I have the code removed 1 line in the file below, while loop using fgets but I do not get it, a desire to help children to run!
https://drive.google.com/open?id=1BUkChJCUK6k9Wiq7Pq0e4amzAHW3vzs_
Not understand what, not understand any portion, command. Ask who knows how to explain how? link not accessible nhé.
Of, hope you understand, I do not understand the conditions of the while loop iteration, How can browse the rows and rows of function fgets?
https://drive.google.com/open?id=1PjJ1nMjL6tctCATTVlN0Ksd29oWLtQVf
fgets is read 1 Chain (here can be seen as 1 current).
while( fgets(str, 199, end) != NULL) mean while, then continue reading.
Am so mean fgets to read characters' n’ then loop back again sir?
e spent visual 2017, getline command gets to spend all faulty e not found with unknown dinh.Va with scanf printf command to add ” _s ” behind it not an error, e to do here, Perhaps not by faulty software.
Ko nhé. Because it's VS. here your code using Dev-C ++, VS can command structure is a little different.
If using gets_s, then this syntax will work for you
gets_s(,
);5 stars for posts, I have fixed the error. Thanks very much