printf vs fprintf 
Author Message
 printf vs fprintf

An interesting point came to mind as I was working today.  I thought I might
share it with those of you who are (or know someone who is) learning the
C language.  It seems to me that a great deal of confusion could be saved
when learning printf, scanf, fprintf, fscanf, sprintf, and sscanf.  The
point is really simple, don't use printf and scanf.  Use, instead,
fprintf (stdout, "....") and fscanf (stdin, "....", &whatever).  Why, you
ask??  Well, if you think about it, scanf and printf are really special
cases of fscanf and fprintf.  The mental correlation is easier if you remember
you always operate on an IO path, represented by some file.  (stdin and stdout
are just standard file, right? - hence Fscanf and Fprintf)  Then, when the
user wants to use sprintf and sscanf, he or she need only remember the the
"IO path" is a stream, a buffer is you will.  After a person has a complete
understanding of how all this fits together, he or she could easily use the
shorter, easier scanf and printf.  Does anyone else think this seems like a
good way to learn the rudiments of IO in C??  Flames are welcome.   ;-)

--
================================================================================

Rochester Institute of Technology, Rochester, NY
    "I got an allergy to Perrier, daylight, and responsibility", Marillion



Sat, 17 Oct 1992 00:57:00 GMT  
 printf vs fprintf
...An interesting point came to mind as I was working today.  I thought I might
...share it with those of you who are (or know someone who is) learning the
...C language.  It seems to me that a great deal of confusion could be saved
...when learning printf, scanf, fprintf, fscanf, sprintf, and sscanf.  The
...point is really simple, don't use printf and scanf.  Use, instead,
...fprintf (stdout, "....") and fscanf (stdin, "....", &whatever).  Why, you
...ask??  Well, if you think about it, scanf and printf are really special
...cases of fscanf and fprintf.  The mental correlation is easier if you remember
...you always operate on an IO path, represented by some file.  (stdin and stdout
...are just standard file, right? - hence Fscanf and Fprintf)  Then, when the
...user wants to use sprintf and sscanf, he or she need only remember the the
..."IO path" is a stream, a buffer is you will.  After a person has a complete
...understanding of how all this fits together, he or she could easily use the
...shorter, easier scanf and printf.  Does anyone else think this seems like a
...good way to learn the rudiments of IO in C??  Flames are welcome.   ;-)

But that adds an incredible burden to the beginner -- thinking about
streams and the additional xprintf/scanf stuff in addition to getting
used to the notation of C.  No flame here, but I do think it's a bad
idea.  At the end of the course, of course, one admits that
printf()/scanf() are special cases of fprintf()/fscanf() -- of course!  :-)



Sat, 17 Oct 1992 20:09:00 GMT  
 printf vs fprintf

Quote:

>The
>point is really simple, don't use printf and scanf.  Use, instead,
>fprintf (stdout, "....") and fscanf (stdin, "....", &whatever).

Please, PLEASE don't tell people to use either scanf() or fscanf().
Instead use gets() or fgets(), followed by sscanf().  Code which uses
scanf() or fscanf() is susceptible to a {*filter*} problem if the input
stream is not correctly formatted.  The problem is that these functions
do not stop reading input at end of line.  They only stop when each %
item in the control string has been matched (or EOF is found).  If the
user fails to enter all the fields expected by the control string, the
(f)scanf() function will assume that the missing data is on the
following line(s).

If this happens with scanf(), the program appears to hang -- it is
waiting for the user to enter that next line, but the user won't because
he is waiting for the results of the line he just entered (unaware that
the entered line is incomplete).

If the input stream for fscanf() is incorrect, the program will not
(typically) hang, but it will mung the input lines together in crazy
ways, and not report any error.

Using (f)gets() followed by sscanf() is much more reliable, since format
errors can be detected and reported immediately.

Quote:

>Rochester Institute of Technology, Rochester, NY

Jesse Perry     (RIT '87 :-)
  UUCP: uunet!proxftl!jesse




Sat, 17 Oct 1992 13:44:00 GMT  
 printf vs fprintf

not to use scanf().  To avoid misunderstandings, I would rephrase it thus:

The scanf() family is for *free-format* input -- non-significant whitespace,
including newline, tends to be ignored.  If you need line-oriented (e.g.
prompted) input, you probably want to use something else.

Quote:
>Using (f)gets() followed by sscanf() is much more reliable, since format
>errors can be detected and reported immediately.

Let me add that fgets() is better than gets() in that it does bounds-checking
(but even so, programs seldom do anything reasonable when it is detected).
Also, when using fgets/sscanf it's easy to overlook the error of having *too
much* data on one input line -- one way to catch this is to put " %c" at the
end of the format (and an appropriate dummy variable in the arglist), and
expect the return value to be one *less* than the number of variables passed.




Sat, 17 Oct 1992 23:13:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. printf vs fprintf

2. (void)printf() vs. printf()

3. Re(2): Re(2): (void)printf() vs. printf()

4. Re(2): (void)printf() vs. printf()

5. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

6. Differences between printf and fprintf plus stdout

7. replacing printf with my printf, then calling C lib's printf

8. cout vs printf()

9. printf %p vs. %x

10. 2 printf's vs. a ternary

11. cout vs printf

12. printf vs. vprintf

 

 
Powered by phpBB® Forum Software