Transcript io9

Advanced UNIX
240-491 Special Topics in Comp. Eng. 1
Semester 2, 2000-2001
9. Standard I/O in C
 Objectives
of these slides:
– look in some detail at standard input
and output in C
240-491 Adv. UNIX:io/9
1
Overview
1. Background
2. Formatted Output: printf()
3. Formatted Input: scanf()
4. Line I/O: gets(), puts()
5. Character I/O: getchar(), putchar()
240-491 Adv. UNIX:io/9
2
1. Background
 Most
I/O library functions are in stdio.h
 Basic
methods:
– 1. Formatted I/O
– 2. Line by line
– 3. Character by character
240-491 Adv. UNIX:io/9
3
2. Formatted Output: printf()


int i = 2, j = 7;
printf(“i = %d, j = %d\n”, i, j);
float, double
printed with %f, %e, %g:
double x = 3.14;
printf(“%f, %e, %g\n”, x, x, x);
3.140000, 3.140000e+00, 3.14
6 dp is the default
240-491 Adv. UNIX:io/9
4
Character Strings

char name[SIZE], ch;
:
printf(“Hi, there, %s\n”, name);
printf(“The letter is %c \n”, ch);
See man ascii for
some unusual chars,
e.g. '\a' '\b' '\r'
240-491 Adv. UNIX:io/9
5
Less Used printf() Format Specifiers
 Specifier
useful for
printing
unusual
chars, see
man ascii
240-491 Adv. UNIX:io/9
%%
%u
%o
%x
%p
%n
Meaning
% character
Decimal unsigned integer
Octal integer
Hexadecimal integer
Pointer (decimal or hex)
Store the number of chars
written so far
continued
6
Examples

int i = 125, num;
int *p = &i;
upper case
hex letters
printf(“%d, %o, %x, %X, %p%n\n”,
i, i, i, i, p, &num);
printf(“num = %d\n”, num);
125, 175, 7d, 7D, 7fffbe48
num = 31
240-491 Adv. UNIX:io/9
the decimal is
converted
7
Four Types of Format Modifiers
1. Flags
– e.g. "%+d"
printf(".....", x, y, z);
format string
2. Field Widths
– e.g. "%10d"
3. Precision
– e.g. "%.4d"
4. Type Modifiers
– e.g. "%ld"
240-491 Adv. UNIX:io/9
8
2.1. Flags
 Flag
Meaning
+
Prefix positive numbers with a + sign
space
Prefix positive numbers with a space
0
Pad with leading zeros
#
Alternate output form
240-491 Adv. UNIX:io/9
9
Examples

printf(“%+d\n%+d\n”, 786, -786);
+786
-786

printf(“% d\n% d\n”, 547, -547);
547
-547

printf(“%+09d\n%09d\n”, 452, 452);
+00000452
000000452
240-491 Adv. UNIX:io/9
useful for
tidy columns
10
Meaning of #
 Prefix 0
is added to octal output (%#o).
 Prefix 0x
or 0X is added to hexadecimal
output (%#x or %#X).

%#e, %#f, %#g have a decimal point, even
when there are no digits after it.

%#g does not remove trailing zeros.
240-491 Adv. UNIX:io/9
11
# Examples

int c = 1427;
float p = 1427.0;
printf(“%#o, %#x, %#X, %g, %#g\n”,
c, c, c, p, p);
02623, 0x593, 0X593, 1427, 1427.00
octal
240-491 Adv. UNIX:io/9
hex
hex
remove '.'
and 0's
do not
remove '.'
and 0's
12
2.2. Field Widths
 Specify
minimum number of characters
printed:
– if too few characters, right justify output
– if too many, ignore field width
240-491 Adv. UNIX:io/9
13
Example
- = left
justify
char *s = “hello”;
width
int x = 23;
printf(“%10d%10s\n”, x, s);
printf(“%-10d%-10s\n”, x, s);
10
23
240-491 Adv. UNIX:io/9
23
hello
10
hello
14
Field Width (*) in printf()
 Calculate
field width based on the next
printf() argument:
#define FIELD_WIDTH 10
printf(“%*d\n”, FIELD_WIDTH, val);
uses FIELD_WIDTH
240-491 Adv. UNIX:io/9
15
2.3. Precision (.) in printf()
 Specifier
Effect
%d
Min. no. of digits
(pad with leading zeros)
%e, %f
No. of digits after the decimal point
%g
No. of significant digits
%s
Min. no. of characters from a string
240-491 Adv. UNIX:io/9
16
Example
int i = 873;
float f = 123.94536;
char s[] = “Happy Birthday”;
printf(“%.4d\n%.9d\n”, i, i);
printf(“%.3f\n%.3e\n%.3g\n”, f, f, f);
printf(“%.11s\n”, s);
0873
000000873
123.945
1.293e+02
124
Happy Birth
240-491 Adv. UNIX:io/9
17
Meaning of *.*
 Calculate
field width and precision from the
next arguments of the printf()
printf(“%*.*s\n”,
field_width, precision, str);
240-491 Adv. UNIX:io/9
18
2.4. Type Modifiers
 Place
h or l before d, o, x, or u
 Place
L before e, f, or g
 Letter
h
l
L
240-491 Adv. UNIX:io/9
Effect
Specify short type
Specify long type
Specify long double type
19
3. Formatted Input: scanf()

int i, x, y;
float val;
char str[100];
scanf(“%d”, &i);
scanf(“%f”, &val);
scanf(“%s”, str);
23
2.3
hi -5 62
/* read in integer */
/* read in float */
/* read in string */
scanf(“%d%d”, &x, &y);
/* read space-separated integers */
240-491 Adv. UNIX:io/9
20
Notes

%d will read a sign (e.g. -27)

%f will accept a float in fractional or
exponential form (e.g. 2.3, 1.2e+02)

scanf() will ignore whitespace to get
to a number
– scanf("%d", &x);
– whitespace includes new lines
240-491 Adv. UNIX:io/9
-27
continued
21
hello
andy

%s only reads non-whitespace characters
– scanf("%s %s", str1, str2);
– skips whitespace to start of text
– stops reading in text when it gets to a
whitespace
– adds ‘\0’
– the string variable (e.g. str1) must have
enough memory
240-491 Adv. UNIX:io/9
continued
22

scanf("%d%d", &x, &y);
same as
 scanf(“%d”, &x);
scanf(“%d”, &y);
27
13
scanf() will read x and y values from
the same line, or any distance apart, so
long as the values are separated by
whitespace.
240-491 Adv. UNIX:io/9
continued
23

%c will read any character (even
whitespace):
scanf(“%c”, &ch);
 Other
characters in the scanf() format
string are used for input matching:
scanf(“%d-%d %d”, &x, &y, &z);
27-13 5
240-491 Adv. UNIX:io/9
continued
24

leaves non-matching characters
on the input stream
scanf()
scanf("%d", &x);
scanf("%s", str);

hello
returns the number of bound
variables, or EOF at end-of-file.
scanf()
int num;
num = scanf("%d %d", &x, &y);
27 13
240-491 Adv. UNIX:io/9
continued
25

scanf()
uses line buffering
– when scanf() is reading from the keyboard, it
only gets a line to process when a RETURN is
pressed
240-491 Adv. UNIX:io/9
26
3.1. Assignment Supression (*)
int month, day, year;
printf(“Enter date as mm-dd-yy: “);
scanf(“%d%*c%d%*c%d”, &month,
&day, &year);
printf(“month = %d day = %d year = %d\n”,
month, day, year);
Enter date as mm-dd-yy: 11-18_97
month = 11 day = 18 year = 97
240-491 Adv. UNIX:io/9
27
3.2. Field Widths
 A field
width specifies the maximum
number of input characters to process
(ignoring whitespace before the start).

int x, y;
scanf(“%2d%d”, &x, &y);
printf(“Integers are: %d, %d\n”, x, y);
123456
Integers are: 12, 3456
240-491 Adv. UNIX:io/9
28
3.3. Scan Sets [...]
 Specify

characters that can be read.
char z[9];
printf(“Enter String: “);
scanf(“%[aeiou]”, z);
printf(“The input was \”%s\”\n”, z);
Enter String: ooeeooahah
The input was “ooeeooa”
240-491 Adv. UNIX:io/9
29
Inverted Scan Sets [^...]
 Specify

characters not to be read.
char z[9];
printf(“Enter String: “);
scanf(“%[^aeiou]”, z);
printf(“The input was \”%s\”\n”, z);
Enter String: Phreak
The input was “Phr”
240-491 Adv. UNIX:io/9
30
3.4. Handling Incorrect Input
 Things
to remember:
– scanf() returns when it sees a character
that it does not want. It leaves the
character on the input stream;
– scanf() returns the number of variables
it has bound
240-491 Adv. UNIX:io/9
continued
31
/* Try to read two integers */
#include <stdio.h>
* supresses assign;
[^\n] matches upto
newline (whitespace)
int main()
{ int i, j, count;
do {
printf(“Enter two integers: “);
count = scanf(“%d %d, &i, &j);
if (count < 2) {
/* somthing wrong */
scanf(“%*[^\n]”);
/* skip to end of line */
printf(“Incorrect. Try again: “);
}
} while (count < 2);
/* until valid input */
:
}
240-491 Adv. UNIX:io/9
32
4. Line I/O

int puts(char *line);
– e.g. puts(“Hello World”);
– Adds a newline to its output.
– If there is an error it returns EOF;
If okay it returns a non-negative integer.
240-491 Adv. UNIX:io/9
33

char *gets(char *line);
– e.g: gets(str);
Bad style:
avoid gets()
– Reads to a newline in stdin;
replaces newline by ‘\0’ in input argument
– gets() returns a NULL when EOF or an error
occurs.
– Main problem: gets() may read in a bigger
string than can be stored in the variable;
use fgets() instead.
240-491 Adv. UNIX:io/9
34

char *fgets(char *line, int n,
FILE *stream);
– e.g: fgets(str, 80, stdin);
– Reads at most n-1 chars into line, stopping if
a newline is encountered;
– the newline is included in line, and a ‘\0’ is
added
– fgets() returns a NULL when EOF or an error
occurs.
240-491 Adv. UNIX:io/9
35
sscanf()

Very useful for separating
reading from processing
in code.
int sscanf(char *s,
const char *format, ...);
 Same
as scanf() but its input comes from
the string argument (s) not from stdin.
240-491 Adv. UNIX:io/9
36
Read then Process

#include <stdio.h>
int main()
{ char str[120];
/* magic number: poor style! */
int i, j;
while (fgets(str,120,stdin) != NULL) { /* read */
/* process */
if (sscanf(str, “%d %d”, &i, &j) == 2)
printf(“i = %d j = %d\n”, i, j);
else if (sscanf(str, “%d”, &i) == 1)
printf(“i = %d j = ??\n”, i);
else
printf(“Could not understand anything!\n”);
:
}
240-491 Adv. UNIX:io/9
37
5. Character I/O

int putchar(int c);
– e.g.:

putchar(‘\n’);
putchar(32);
int getchar(void);
– e.g.:
int ch;
ch = getchar();
while ((ch = getchar()) != EOF)
...
Remember the brackets.
240-491 Adv. UNIX:io/9
38
Cannot “Press any key”
#include <stdio.h>
int main()
{ char ch;
printf(“Press any key to go on...”);
ch = getchar(); /* type conversion */
printf(“You pressed ‘%c’\n”, ch);
:
}
Problem: UNIX line buffering
 Quick fix: say “Press RETURN”

240-491 Adv. UNIX:io/9
39