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
> 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...
> 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.
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 / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
fputs writes a string to the output stream, and you have to know the
string up front; whereas the data that fprintf writes can be built at
run-time:
Contrast:
fputs("now is the time for all good men to party\n", stderr);
with:
fprintf(stderr,
"now is the time for %d good men to party\n",
goodmenqty);
<snip>
> Does using one function depend on only programmer's taste?
No, they have different purposes. If you just want to write a single
fixed string to the output stream, fputs can do this quickly and without
fuss. But if your data needs are more complex, fputs isn't up to it, and
that's the time to look at fprintf.
(Some people just use fprintf for everything and be done with it. That's
a perfectly workable strategy.)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
Thank you very much. I have bookmarked c-faq link. I will give a look
at it.
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.
fputs is a much simpler function than fprintf is.
I once advised a colleague that he might be able to shrink his code size
if he was able to replace all of his fprintf calls with fputs calls.
It worked, which was good because at that time
he did need to shrink his code size.
--
pete
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
> 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
Another important difference is that
puts writes a newline at the end, while fputs doesn't.
--
pete
No? That's crazy then, why make fputs() and puts() behave differently.
--
Bartc
I don't know why.
N869
7.19.7.4 The fputs function
Synopsis
[#1]
#include <stdio.h>
int fputs(const char * restrict s,
FILE * restrict stream);
Description
[#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.
--
pete
> [#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
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:)
That would be very, very silly - a bit like crossing the road with your
eyes shut.
> 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].
Which is the reason you don't give attackers control of the format string.
>
> [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.
Yeah. I started reviewing his stuff in considerable detail, but he
seemed to lose interest after the Nth crit (for large N).