Why use fputs?

This is a discussion on Why use fputs? within the C Programming forums, part of the General Programming Boards category; why?...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    Why use fputs?

    why?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To put a line to a file stream. Duh!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    To print a line of text to the screen, you might want to use it instead of puts() because it doesn't append a newline.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    puts() appends a newline.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That was my point.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    Thanks for the answers but to make the questions more clear.Why not use the conventional printf.BTW Quzah i dont understand what you said

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Because printf always goes to stdout, but fputs can go to any file, of course.
    Now a better question might be "why use fputs instead of fprintf?" and the answer to that is, it's simpler, and therefore somewhat quicker and somewhat smaller (which is a win if you're statically linking your libc).
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    fputs() doesn't have to parse the string "%s" to figure out that all you want to do is print a string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In other words, you have to prepare your string ahead of time for fputs, where fprintf allows you to format at the time of outputting.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    If you're just trying spit out a simple message that doesn't need formatting like "You win!" then puts()/fputs() is more efficient.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fputs only write first part of string
    By jamie85 in forum C Programming
    Replies: 4
    Last Post: 11-17-2005, 07:18 AM
  2. Question about fputs
    By nizbit in forum C Programming
    Replies: 6
    Last Post: 03-03-2005, 06:19 PM
  3. fputs
    By barim in forum C Programming
    Replies: 6
    Last Post: 07-31-2004, 02:41 AM
  4. fputs and fprintf
    By fosca in forum Linux Programming
    Replies: 2
    Last Post: 12-17-2001, 06:15 AM
  5. fputs and sequential file
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 03:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21