fputs and fprintf

J

_JusSx_

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-
 
S

santosh

_JusSx_ said:
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.
 
S

Seebs

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
 
J

_JusSx_

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


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-
 
B

bartc

_JusSx_ said:
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).
 
B

bartc

pete said:
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.
 
S

Stefan Ram

_JusSx_ said:
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
 
B

bartc

pete said:
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.
 
S

santosh

bartc said:
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:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top