hey.
when I use gcc to compile my .c-files it complains about the gets library function and says that "it is dangerous and should not be used". anyone know why?
dagH
hey.
when I use gcc to compile my .c-files it complains about the gets library function and says that "it is dangerous and should not be used". anyone know why?
dagH
Read the FAQ regarding gets() for a full explaination...
But anyway, by using gets() you risk having characters left in the input stream (stdin), usually new lines after you've taken what you've asked for out. gets() can be used, providing you know the risks behind it... You also run the risk of overflowing the buffer you specify, since gets() doesn't ask how bit it is (it just assumes it can hold all the input).
You can use fgets() instead so you can avoid buffer over-runs and new-line issues.
Last edited by zacs7; 04-29-2007 at 05:29 AM.
Because it's dangerous and shouldn't be used? gets() doesn't allow you to stop reading when the array is full, so can end up writing past the end if the array and breaking all kinds of things. Use fgets() instead because it lets you pass a limit to the number of characters it can read.anyone know why?
gets() doesn't have that problem at all. It always reads a full line, including the '\n'.by using gets() you risk having characters left in the input stream (stdin), usually new lines after you've taken what you've asked for out.
No it doesn't - the trailing \n is left in the stream,
Unless it's read but not included...?Originally Posted by http://www.cplusplus.com/reference/clibrary/cstdio/gets.html
You get a gold star. gets() removes the '\n' from the stream but doesn't store it in the array. The standard tells you that, but you can also test it with a quick program:Unless it's read but not included...?
The getchar() call will block because there's nothing in the stream to read, not even a '\n'.Code:#include <stdio.h> int main( void ) { char s[BUFSIZ]; gets( s ); puts( "Done with gets()" ); getchar(); puts( "Done with getchar()" ); return 0; }