954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C Function

Hello,
Out of fgets() and gets() which function is safe to use and why?

Thanks

Emma1
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: 6
Solved Threads: 9
 

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)

me_ansh
Light Poster
44 posts since Jan 2009
Reputation Points: 13
Solved Threads: 5
 

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 .

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You