Sunteți pe pagina 1din 16

Standard Input and Output

Overview
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

Communication 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?

stdin, 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

C Program Entrances and Exits


Constants and variables
stdout (Screen)

D
stderr (Screen)

main
stdin (Keyboard)

Operations and
functions

Instructions and
operating procedures

Files 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
45 27 15 52

stdin

Program
to compute
average

Average is 34.75

stdout

The program reads data from stdin and writes


data to stdout as if they were ordinary files

One 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

Hanson Susan

1045.76
98.62

Manager

15

Operator

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'

Functions 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);

Functions 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);

Input 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 '<' sign redirects input from a file
The '>' sign redirects output to a new file
The ">>" sign redirects output and appends the
data to a current file or creates the file if it doesn't
yet exist

Example of I/O Redirection


Input comes from a file, output goes to the screen
C:\myprogram

<numbers.dat

Input comes from the keyboard, output goes to a file


C:\myprogram >results.txt

Input comes from a file; output goes to a file


C:\myprogram <lab.txt >findings.dat

Input comes from the keyboard and the output is


appended to the contents in a file
C:\myprogram >>findings.dat

Some Redirection to Try Out


in MS-DOS
Example #1
C:\dir >dirwords.txt
C:\type dirwords.txt

Example #2

C:\dir >dir.dat
C:\find ".c" <dir.dat
C:\find ".c" <dir.dat >findings.txt
C:\find ".exe" <dir.dat >>findings.txt
C:\type findings.txt

printf 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:
%<fieldwidth>.<precision><conversion specifier>
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

printf 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);

scanf 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: %<conversion specifier>
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

scanf Examples
One format specifier, two arguments
scanf("%d", &theResult);

Three format specifiers, four arguments


scanf("%d%f%s", &anInteger, &aFloat, aString);

Same as above, but in a different order


scanf("%s %f %d", aString, &aFloat, &anInteger);

S-ar putea să vă placă și