fputs and fprintf

Discussion in 'C Programming' started by _JusSx_, Feb 28, 2010.

  1. _JusSx_

    _JusSx_ Guest

    Hi,
    I don't know if the question is in newsgroup FAQ because I haven't found
    it yet and so I haven't read it yet.

    I would like to know what differences are between these two
    C functions: *fputs* and *fprintf*.

    fputs C function has been used much in coreutils C source code while fprintf
    has been used to printf help or error.

    $ grep fputs *.c

    #v+
    base64.c: fputs (_("\
    base64.c: fputs (_("\
    base64.c: fputs (_("\
    base64.c: fputs (_("\
    base64.c: if (fputs ("\n", out) < 0)
    base64.c: if (wrap_column && current_column > 0 && fputs ("\n", out) < 0)
    basename.c: fputs (_("\
    basename.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
    basename.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
    cat.c: fputs (_("\
    cat.c: fputs (_("\
    cat.c: fputs (HELP_OPTION_DESCRIPTION, stdout);
    cat.c: fputs (VERSION_OPTION_DESCRIPTION, stdout);
    cat.c: fputs (_("\
    chcon.c: fputs (_("\
    ....
    #v-

    #v+
    $ grep fprintf *.c

    base64.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
    basename.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
    cat.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
    chcon.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
    chgrp.c: fprintf (stderr, _("Try `%s --help' for more information.\n"),
    ....
    #v-

    Does using one function depend on only programmer's taste?


    Thanks in advance

    -JusSx-

    --
    Linux is only free if your time has no value
    _JusSx_, Feb 28, 2010
    #1
    1. Advertising

  2. _JusSx_

    santosh Guest

    _JusSx_ <> writes:

    > Hi,
    > I don't know if the question is in newsgroup FAQ because I haven't
    > found it yet and so I haven't read it yet.


    Google could've helped you...

    <http://c-faq.com/>

    > I would like to know what differences are between these two
    > C functions: *fputs* and *fprintf*.


    fprintf does formatted output. That is, it reads and interprets a
    format string that you supply and writes to the output stream the
    results. You can use it to print the values of nearly all of C's
    object types.

    fputs simply writes the string you supply it to the indicated output
    stream.

    <http://www.dinkumware.com/manuals/?manual=compleat&page=stdio.html>
    <http://www.dinkumware.com/manuals/?manual=compleat&page=lib_over.html>

    and man fprintf/fputs on your system too.

    > Does using one function depend on only programmer's taste?


    On the programmer's requirements, more than taste. fprintf is the way
    to go for writing out the values of the various supported types,
    unless you have your own version. fputs doesn't do that, but it's
    fine for simply writing C strings.
    santosh, Feb 28, 2010
    #2
    1. Advertising

  3. _JusSx_

    Seebs Guest

    On 2010-02-28, _JusSx_ <> wrote:
    > I would like to know what differences are between these two
    > C functions: *fputs* and *fprintf*.


    One of them formats output, one prints a string without formatting it.

    > Does using one function depend on only programmer's taste?


    No.

    Have you considered the idea of getting some kind of documentation, book,
    or anything like that? If "man fprintf" doesn't tell you anything, your
    system is misconfigured. (I'm assuming something unixy because you're
    looking at coreutils.)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Feb 28, 2010
    #3
  4. _JusSx_

    _JusSx_ Guest

    On 2010-02-28, Seebs <> wrote:
    > On 2010-02-28, _JusSx_ <> wrote:
    >> I would like to know what differences are between these two
    >> C functions: *fputs* and *fprintf*.

    >
    > One of them formats output, one prints a string without formatting it.
    >
    >> Does using one function depend on only programmer's taste?

    >
    > No.
    >
    > Have you considered the idea of getting some kind of documentation, book,
    > or anything like that? If "man fprintf" doesn't tell you anything, your
    > system is misconfigured. (I'm assuming something unixy because you're
    > looking at coreutils.)
    >
    > -s


    Yes, I read fputs and fprintf man pages but maybe I didn't read them
    carefully.

    Now I know the difference is: fprintf prints formatted output, fputs
    doesn't print formatted output.

    I thought fputs printed formatted output like fprint does but I was
    wrong.

    -JusSx-

    --
    Linux is only free if your time has no value
    _JusSx_, Feb 28, 2010
    #4
  5. _JusSx_

    _JusSx_ Guest

    On 2010-02-28, santosh <> wrote:
    > _JusSx_ <> writes:
    >
    >> Hi,
    >> I don't know if the question is in newsgroup FAQ because I haven't
    >> found it yet and so I haven't read it yet.

    >
    > Google could've helped you...
    >
    ><http://c-faq.com/>
    >
    > ...


    Thank you very much. I have bookmarked c-faq link. I will give a look
    at it.

    -JusSx-


    --
    Linux is only free if your time has no value
    _JusSx_, Feb 28, 2010
    #5
  6. _JusSx_

    bartc Guest

    "_JusSx_" <> wrote in message
    news:-september.org...
    > Hi,
    > I don't know if the question is in newsgroup FAQ because I haven't found
    > it yet and so I haven't read it yet.
    >
    > I would like to know what differences are between these two
    > C functions: *fputs* and *fprintf*.


    fputs/puts prints a string, while fprintf/printf prints a (format) string
    *and* any number of other values.

    But one important difference is that puts writes a newline at the end, while
    printf requires you to insert the fiddly \n sequence at the end of the
    string (significant for terrible typists like me, with \ being one of those
    keys that is in a different place on every keyboard).

    --
    Bartc
    bartc, Feb 28, 2010
    #6
  7. On 28 Feb, 08:26, _JusSx_ <> wrote:


    > I don't know if the question is in newsgroup FAQ because I haven't found
    > it yet and so I haven't read it yet.
    >
    > I would like to know what differences are between these two
    > C functions: *fputs* and *fprintf*.


    <snip>

    the FAQ is at
    http://c-faq.com/

    but the FAQ won't help with your question, you need a C library
    reference for that
    http://www.dinkumware.com/manuals/#Standard C Library
    Nick Keighley, Feb 28, 2010
    #7
  8. _JusSx_

    bartc Guest

    "pete" <> wrote in message
    news:...
    > bartc wrote:
    >
    >> fputs/puts prints a string,
    >> while fprintf/printf prints a (format) string
    >> *and* any number of other values.
    >>
    >> But one important difference is that
    >> puts writes a newline at the end, while
    >> printf requires you to insert the fiddly \n sequence at the end of the
    >> string (significant for terrible typists like me,
    >> with \ being one of those
    >> keys that is in a different place on every keyboard).

    >
    > Another important difference is that
    > puts writes a newline at the end, while fputs doesn't.


    No? That's crazy then, why make fputs() and puts() behave differently.

    --
    Bartc
    bartc, Feb 28, 2010
    #8
  9. _JusSx_

    Stefan Ram Guest

    _JusSx_ <> writes:
    >I would like to know what differences are between these two
    >C functions: *fputs* and *fprintf*.


    Remarkable: The answer I would expect to be most often
    did not occur at all so far (in the subset of the posters
    I read):

    fprintf( stderr, userinput );

    An attacker who can fully or partially control the contents
    of a format string can crash a vulnerable process, view the
    contents of the stack, view memory content, or write to an
    arbitrary memory location and consequently execute arbitrary
    code with the permissions of the vulnerable process [Seacord 05a].

    [Seacord 05a] Seacord, Robert C. Secure Coding in C and C++.
    Boston, MA: Addison-Wesley, 2005. See
    http://www.cert.org/books/secure-coding for news and errata.

    http://google.to/search?q=FIO30-C
    Stefan Ram, Feb 28, 2010
    #9
  10. _JusSx_

    bartc Guest

    "pete" <> wrote in message
    news:...
    > bartc wrote:
    >>
    >> "pete" <> wrote in message
    >> news:...
    >>
    >>> bartc wrote:
    >>>
    >>>> fputs/puts prints a string,
    >>>> while fprintf/printf prints a (format) string
    >>>> *and* any number of other values.
    >>>>
    >>>> But one important difference is that
    >>>> puts writes a newline at the end, while
    >>>> printf requires you to insert the fiddly \n sequence at the end of the
    >>>> string (significant for terrible typists like me,
    >>>> with \ being one of those
    >>>> keys that is in a different place on every keyboard).
    >>>
    >>>
    >>> Another important difference is that
    >>> puts writes a newline at the end, while fputs doesn't.

    >>
    >>
    >> No? That's crazy then, why make fputs() and puts() behave differently.

    >
    > I don't know why.


    > [#2] The fputs function writes the string pointed to by s to
    > the stream pointed to by stream. The terminating null
    > character is not written.


    I vaguely remember now fputs/puts had to be compatible with fputs/gets(),
    which retain/don't retain a newline character.

    Still, you would have expected fputs/puts to do the same thing other than
    one takes a file parameter and the other defaults to stdout.

    --
    Bartc
    bartc, Feb 28, 2010
    #10
  11. _JusSx_

    santosh Guest

    bartc <> writes:

    >
    > "pete" <> wrote in message
    > news:...
    >> bartc wrote:
    >>
    >>> fputs/puts prints a string,
    >>> while fprintf/printf prints a (format) string
    >>> *and* any number of other values.
    >>>
    >>> But one important difference is that
    >>> puts writes a newline at the end, while
    >>> printf requires you to insert the fiddly \n sequence at the end
    >>> of the string (significant for terrible typists like me,
    >>> with \ being one of those
    >>> keys that is in a different place on every keyboard).

    >>
    >> Another important difference is that
    >> puts writes a newline at the end, while fputs doesn't.

    >
    > No? That's crazy then, why make fputs() and puts() behave
    > differently.


    I'd guess that the reason was because while it makes sense to advance
    to a newline after printing a line on the standard output (which is
    most often a console), it makes less sense in a file.

    And also, maybe for symmetry with gets/puts and fgets/fputs
    combination. In each pair one reverses the behaviour of the other, in
    terms of adding or discarding a newline.

    And it's useful to have a function that can write strings without
    adding characters on it's own. Sure fputc and fprintf are there, but
    while the former is too primitive, the latter adds overhead for just
    writing a string.

    All the above just my naive guesses:)
    santosh, Feb 28, 2010
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xoinki
    Replies:
    1
    Views:
    376
  2. dave
    Replies:
    4
    Views:
    660
  3. Chad

    A question about fputs()

    Chad, Jun 22, 2008, in forum: C Programming
    Replies:
    17
    Views:
    653
    Keith Thompson
    Jun 23, 2008
  4. Petr Gotthard

    fputs in tp_print crashes under Win32

    Petr Gotthard, Nov 12, 2008, in forum: Python
    Replies:
    1
    Views:
    298
    Gabriel Genellina
    Nov 15, 2008
  5. hassan

    fputs

    hassan, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    261
    Ulrich Eckhardt
    Jun 5, 2012
Loading...

Share This Page