Hello,
Out of fgets() and gets() which function is safe to use and why?
Thanks
Hello,
Out of fgets() and gets() which function is safe to use and why?Thanks
It depends where you are using :
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.
gets collects a string of characters terminated by a new line from the
standard input stream stdin and puts it into s.
Also fgets() takes 'number of bytes to be copied' as an argument, which is a very good practice as it never lets you in problems like 'buffer overflows'...Use of gets() is generally discouraged..
For further info: man fgets (if you are on linux)
gets() is NEVER safe to use because it can corrupt the memory of your program and cause it to crash (or coredump in *nix). Read this explaination.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.