ansi c: gets dangerous
-
Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.
char *Comando=0;
Comando=(char*)malloc(150*sizeof(char));gets(Comando);
I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!
-
Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.
char *Comando=0;
Comando=(char*)malloc(150*sizeof(char));gets(Comando);
I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!
The problem with
gets()
is that is doesn't consider the size of the destination buffer. In your case just enter more that 150 characters and PRESTO! Insta-crash(tm). May I recomandfgets()
instead? And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows -
Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.
char *Comando=0;
Comando=(char*)malloc(150*sizeof(char));gets(Comando);
I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!
That warning is telling you that it is IMPOSSIBLE to use gets correctly! If your program uses gets I can crash it. Any professor who sees gets in code should automaticly give the student an F! instead you should use fgets, which can replace your use of gets like this: fgets(Comando,150,STDIN); See, it is easy to use fgets, and I can no longer crash your program just be doing something stupid.
-
Hi, i'm doing an ansi c program, compiling with gcc. I get a warning that the gets function is dangerous. Everything works fine, but i don't want to get that warning.
char *Comando=0;
Comando=(char*)malloc(150*sizeof(char));gets(Comando);
I can't (or don't know how to) use scanf as if i type a string with spaces scanf only saves the characters until the first space. which function could i use apart from gets (and it must be ansi c )? thanks!
There is an extension to the
scanf(...)
function that allows to scan non-space-delimited strings. I do not know if it is available in GCC's libraries... It works like this: to scan a string that is terminated by a semicolon, your format specifier would be:%[^;]
. For example, if you wanted to scan this string:C:12345;B:09876
And extract the two numeric values into two strings (12345
and09876
), you would write something like:char caStringBuf1[ 64 + 1 ]; char caStringBuf2[ 64 + 1 ]; sscanf( "C:12345;B:09876","**C:%[^;];B:%s**", caStringBuf1, caStringBuf2 ); caStringBuf1[ 64 ] = '\0'; caStringBuf2[ 64 ] = '\0';
You would want to limit the input test somehow, of course and add some validation code, but that would scan the string correctly. Peace! -=- James Tip for inexperienced drivers: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!
Articles -- Products: Delete FXP Files & Check Favorites -
That warning is telling you that it is IMPOSSIBLE to use gets correctly! If your program uses gets I can crash it. Any professor who sees gets in code should automaticly give the student an F! instead you should use fgets, which can replace your use of gets like this: fgets(Comando,150,STDIN); See, it is easy to use fgets, and I can no longer crash your program just be doing something stupid.