943,650 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 555
  • C RSS
Jan 22nd, 2009
0

C Function

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

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Emma1 is offline Offline
3 posts
since Jan 2009
Jan 22nd, 2009
0

Re: C Function

Click to Expand / Collapse  Quote originally posted by Emma1 ...
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.
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008
Jan 22nd, 2009
1

Re: C Function

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)
Last edited by me_ansh; Jan 22nd, 2009 at 7:52 am.
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009
Jan 22nd, 2009
0

Re: C Function

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: compiler error when address of a pointer is passed
Next Thread in C Forum Timeline: Network Programming Questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC