2.1.3 What can I use instead of gets()?
Until I publish my booklet on console programming. I suggest using the fgets() function as a temporary solution and replacement for gets(). Here is the format:
fgets(buffer,size,stdin);
buffer is the location of your string storage space or buffer.
size is the number of characters to input. This sets a limit on input and therefore it's not "dangerous" as gets() is.
stdin is the standard input devices, the filename for the keyboard.
So:
Code:
#include <stdio.h>
int main()
{
char buffer[128];
printf("Type something: ");
fgets(buffer,127,stdin);
printf("You typed %s\n",buffer);
return(0);
}
Note that fgets(), unlike gets() also reads in the carriage return (Enter key) at the end of the line. That character becomes part of the string you input. That may screw things up a bit, but it's a simple workaround and avoids the Linux "gets() is dangerous" warning.