Title: Standard Input and Output
1Standard Input and Output
2Overview
- Data communication with a C program and the
outside world is performed through files - Files are a non-volatile way to store data by
means of such media as tape, CD-ROM, floppy disk,
ZIP disk, hard drive, etc. - C (just like the UNIX operating system) considers
all process communication media to be files - An ordinary file on a disk is considered to be a
file - So is the keyboard, the screen, parallel ports,
and serial ports
3Communication through Files
- Programs access files through basic file
operations - Open a file
- Read data from a file
- Write data to a file
- Close a file
- Text files store all data types as character
bytes the way a program reads the data (either
text or binary mode) determines how the data is
interpreted - Example 12 z rti 456.79 room
- character?, character string?, integer?, floating
point number?
4stdin, stdout, stderr
- Three files are automatically opened each time a
C program is run and automatically closed when a
C program ends stdin, stdout, and stderr - stdin
- Standard input, default source is the keyboard
- stdout
- Standard output, default destination is the
screen - stderr
- Standard error, default destination is the screen
- These three variable names are referred to as
file descriptors each is a handle (or pointer)
to a record (i.e., struct) describing the actual
file
5C Program Entrances and Exits
Constants and variables
main
stdout (Screen)
A
B
C
D
stderr (Screen)
stdin (Keyboard)
X
Y
Z
Instructions and operating procedures
Operations and functions
6Files as Pipes for Data
Files act as pipes to bring a stream of data into
the program and send a stream of data out of the
program
Program to compute average
45 27 15 52
Average is 34.75
stdin
stdout
The program reads data from stdin and writes data
to stdout as if they were ordinary files
7One Byte At a Time
Data is read or written one byte at a time from a
file until the end of the file is reached or
until the file is closed. The file system uses
a pointer to keep track of the next byte to read
or to write
Smith Jack 1045.76 Manager 15 Hanson
Susan 98.62 Operator 7 Jones Nancy
790.25 Administrator 10 Doe Carl 526.71
Technician 12
In this file, the program has read the data as
far as the letter 'J'. The next character to be
read is 'a'
8Functions Using stdin and stdout-1
- Some standard C functions implicitly use stdin
and stdout - scanf("d", aNumber)
- aSymbol getchar()
- gets(theBuffer) // High security risk
- printf("Average 5.2f", theAverage)
- putchar(aCharacter)
- puts(aPhrase)
9Functions Using stdin and stdout-2
- Other functions need to have stdin and stdout
specified as the file descriptor - fscanf(stdin, "d", aNumber)
- aSymbol fgetc(stdin)
- fgets(theBuffer, sizeof(theBuffer), stdin)
- fprintf(stdout, "Average 5.2f",
theAverage) - fputc(aCharacter, stdout)
- puts(aPhrase, stdout)
10Input and Output Redirection
- Because a C program considers stdin and stdout to
be ordinary files, a user can redirect the input
and output to another source or destination - That source or destination can be an I/O device
or an ordinary file - This redirection can be done when a program is
run from the DOS or UNIX command line - The 'lt' sign redirects input from a file
- The 'gt' sign redirects output to a new file
- The "gtgt" sign redirects output and appends the
data to a current file or creates the file if it
doesn't yet exist
11Example of I/O Redirection
- Input comes from a file, output goes to the
screen - C\myprogram ltnumbers.dat
- Input comes from the keyboard, output goes to a
file - C\myprogram gtresults.txt
- Input comes from a file output goes to a file
- C\myprogram ltlab.txt gtfindings.dat
- Input comes from the keyboard and the output is
appended to the contents in a file - C\myprogram gtgtfindings.dat
12Some Redirection to Try Outin MS-DOS
- Example 1
- C\dir gtdirwords.txt
- C\type dirwords.txt
- Example 2
- C\dir gtdir.dat
- C\find ".c" ltdir.dat
- C\find ".c" ltdir.dat gtfindings.txt
- C\find ".exe" ltdir.dat gtgtfindings.txt
- C\type findings.txt
13printf Function
- The printf function writes formatted output to
stdout - Ex. printf("Average 5.2f \n", theAverage)
- The first argument is a character string
containing ordinary text, escape characters, and
format specifiers - Syntax for format specifier ltfieldwidthgt.ltprecis
iongtltconversion specifiergt - Each escape character is a combination of a '\'
and a character, which represents a nonviewable
ASCII character. An example is \n for newline - The order, number, and type of the format
specifiers corresponds to the order, number, and
type of the arguments in the printf call
14printf Examples
- No format specifiers, one escape character, no
other arguments - printf("Name Age Address Distance\n")
- One format specifier, two arguments
- printf("Results is d\n", theResult)
- Four format specifiers, five arguments
- printf("c d f s", aCharacter, anInteger,
aFloat, aString) - Same as above, but with field width and precision
specified - printf("4c 3d 5.3f 8s", aCharacter,
anInteger, aFloat, aString)
15scanf Function
- The scanf function reads formatted input from
stdin - scanf("d", aNumber)
- The first argument is a character string
containing one or more conversion specifiers - Syntax for format specifier ltconversion
specifiergt - The order, number, and type of the conversion
specifiers corresponds to the order, number, and
type of the other arguments in the scanf call - Each argument value must by the address of the
variable, NOT the variable itself, where the
value should be stored - To designate the address for a character,
integer, structure, or floating point variable,
precede the variable name by the operator,
which means "address of - Array names are a special case when passed as an
argument
16scanf Examples
- One format specifier, two arguments
- scanf("d", theResult)
- Three format specifiers, four arguments
- scanf("dfs", anInteger, aFloat, aString)
- Same as above, but in a different order
- scanf("s f d", aString, aFloat, anInteger)
?