C Function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 3
Reputation: Emma1 is an unknown quantity at this point 
Solved Threads: 0
Emma1 Emma1 is offline Offline
Newbie Poster

C Function

 
0
  #1
Jan 22nd, 2009
Hello,
Out of fgets() and gets() which function is safe to use and why?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: C Function

 
0
  #2
Jan 22nd, 2009
Originally Posted by Emma1 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: C Function

 
1
  #3
Jan 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C Function

 
0
  #4
Jan 22nd, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC