Sams Teach Yourself C in 21 Days

(singke) #1
Working with the Screen, Printer, and Keyboard 373

14


When you use stdinandstdout, you can redirect program input and output. Input can
come from a disk file rather than the keyboard, and output can go to a disk file or to the
printer rather than to the display screen.
You learned why error messages should be sent to the stream stderrinstead of stdout.
Becausestderris usually connected to the display screen, you are assured of seeing
error messages even when the program output is redirected.
You also learned about two other streams that are not ANSI standard:
stdprn The printer
stdaux The communications port

Q&A ....................................................................................................................


Q What happens if I try to get input from an output stream?
AYou can write a C program to do this, but it won’t work. For example, if you try to
usestdprnwithfscanf(), the program compiles into an executable file, but the
printer is incapable of sending input, so your program doesn’t operate as intended.
Q What happens if I redirect one of the standard streams?
ADoing this might cause problems later in the program. If you redirect a stream, you
must put it back if you need it again in the same program. Many of the functions
described in this chapter use the standard streams. They all use the same streams,
so if you change the stream in one place, you change it for all the functions. For
example, assign stdoutequal to stdprnin one of the listings from today’s lessons
and see what happens.
Q Is there any danger in using non-ANSI functions in a program?
AMost compilers come with many useful functions that aren’t ANSI-standard. If you
plan on always using that compiler and not porting your code to other compilers or
platforms, there won’t be a problem. If you’re going to use other compilers and
platforms, you should be concerned with ANSI compatibility.
Q Why shouldn’t I always use fprintf()instead of printf()? Or fscanf()
instead of scanf()?
AIf you’re using the standard output or input streams, you should use printf()and
scanf(). By using these simpler functions, you don’t have to bother with any other
streams.

22 448201x-CH14 8/13/02 11:12 AM Page 373

Free download pdf