User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,205 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,356 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 312 | Replies: 2
Reply
Join Date: May 2008
Posts: 7
Reputation: nagaa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nagaa nagaa is offline Offline
Newbie Poster

Diff between fread() and fgets()

  #1  
May 15th, 2008
what is the difference between fread() and fgets()?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 274
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 2
Solved Threads: 46
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz in Training

Re: Diff between fread() and fgets()

  #2  
May 15th, 2008
fgets reads a single line of characters, but fread reads a block of unidentified objects. fgets uses '\n' as a delimiter, but fread doesn't inspect any of the objects so it relies on a limit of the number of objects. If you're using fread to read string data, the only two significant differences are:
  1. fgets terminates the string with '\0', but fread does not
  2. fgets uses '\n' as a delimiter as well as an upper limit, but fread only uses an upper limit
Subtlety is the art of saying what you think and getting out of the way before it is understood.
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 670
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 43
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Diff between fread() and fgets()

  #3  
May 15th, 2008
"fgets" is essentially a simplified version of "fread".

"fgets" is good for (and should only be used for) reading character strings from an input stream, be it a file or the stdin device.

"fread" is suited for any data type, such as binary (hex) data. it gives you more rope to hang yourself with, so if you're just wanting to read ascii text, stick with "fgets"

http://www.cplusplus.com/reference/c...dio/fread.html
http://www.cplusplus.com/reference/c...dio/fgets.html
I drink your milkshake.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 2:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC