PDA

View Full Version : use of fgets


usha
02-07-2006, 08:54 AM
hi
please tell me how to use gets , initialization, what ever is needed to use fgets, because when ever i use it i get an error "usage of "gets" is dangerous.
i am in confusion how to use it
thanking u sir
usha

RobSeace
02-07-2006, 12:11 PM
You should never use plain old gets()... It's a security hole, since it has no way of
restricting the size of the input to the size of your buffer, so it's trivial for anyone to
cause a buffer overflow... Just use fgets() instead... It does the same exact job,
but it's safe, since you pass in the buffer size... I'm not sure what your confusion
about using it is... The usage is pretty straightforward:


char buf[256];

while (fgets (buf, sizeof (buf), stdin)) {
fputs (buf, stdout);
}