PattPatelCh18.ppt
Chapter 18
I/O in C
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-2
Standard C Library
I/O commands are not included as part of the C language.
Instead, they are part of the Standard C Library.
• A collection of functions and macros
that must be implemented by any ANSI standard implementation.
• Automatically linked with every executable.
• Implementation depends on processor, operating system, etc.,
but interface is standard.
Since they are not part of the language,
compiler must be told about function interfaces.
Standard header files are provided,
which contain declarations of functions, variables, etc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-3
Basic I/O Functions
The standard I/O functions are declared in the
Function Description
putchar Displays an ASCII character to the screen.
getchar Reads an ASCII character from the keyboard.
printf Displays a formatted string,
scanf Reads a formatted string.
fopen Open/create a file for I/O.
fprintf Writes a formatted string to a file.
fscanf Reads a formatted string from a file.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-4
Text Streams
All character-based I/O in C is performed on text streams.
A stream is a sequence of ASCII characters, such as:
• the sequence of ASCII characters printed to the monitor
by a single program
• the sequence of ASCII characters entered by the user
during a single program
• the sequence of ASCII characters in a single file
Characters are processed in the order in which
they were added to the stream.
• E.g., a program sees input characters in the same order
as the user typed them.
Standard input stream (keyboard) is called stdin.
Standard output stream (monitor) is called stdout.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-5
Character I/O
putchar(c) Adds one ASCII character (c) to stdout.
getchar() Reads one ASCII character from stdin.
These functions deal with “raw” ASCII characters;
no type conversion is performed.
char c = ‘h’;
…
putchar(c);
putchar(‘h’);
putchar(104);
Each of these calls
prints ‘h’ to the screen.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-6
Buffered I/O
In many systems, characters are buffered in memory
during an I/O operation.
• Conceptually, each I/O stream has its own buffer.
Keyboard input stream
• Characters are added to the buffer only when the
newline character (i.e., the “Enter” key) is pressed.
• This allows user to correct input before confirming with Enter.
Output stream
• Characters are not flushed to the output device
until the newline character is added.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-7
Input Buffering
printf(“Input character 1:\n”);
inChar1 = getchar();
printf(“Input character 2:\n”);
inChar2 = getchar();
• After seeing the first prompt and typing a single
character, nothing happens.
• Expect to see the second prompt, but character not
added to stdin until Enter is pressed.
• When Enter is pressed, newline is added to stream
and is consumed by second getchar(),
so inChar2 is set to’\n’.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-8
Output Buffering
putchar(‘a’);
/* generate some delay */
for (i=0; i
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-19
fopen
The fopen (pronounced “eff-open”) function associates a
physical file with a stream.
FILE *fopen(char* name, char* mode);
First argument: name
• The name of the physical file, or how to locate it on the
storage device. This may be dependent on the underlying
operating system.
Second argument: mode
• How the file will be used:
“r” — read from the file
“w” — write, starting at the beginning of the file
“a” — write, starting at the end of the file (append)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18-20
fprintf and fscanf
Once a file is opened, it can be read or written
using fscanf() and fprintf(), respectively.
These are just like scanf() and printf(),
except an additional argument specifies a file pointer.
fprintf(outfile, “The answer is %d\n”, x);
fscanf(infile, “%s %d/%d/%d %lf”,
name, &bMonth, &bDay, &bYear, &gpa);