Advanced UNIX 240 491 Special Topics in Comp

  • Slides: 39
Download presentation
Advanced UNIX 240 -491 Special Topics in Comp. Eng. 1 Semester 2, 2000 -2001

Advanced UNIX 240 -491 Special Topics in Comp. Eng. 1 Semester 2, 2000 -2001 . 9 Standard I/O in C v 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:

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 v Most I/O library functions are in stdio. h v Basic

. 1 Background v Most I/O library functions are in stdio. h v 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() v v int i = 2, j = 7;

. 2 Formatted Output: printf() v v int i = 2, j = 7; printf(“i = %d, j = %dn”, i, j; ( float, double printed with %f, %e, %g: double x = 3. 14; printf(“%f, %e, %gn”, x, x, x); 3. 140000, 3. 140000 e+00, 3. 14 6 dp is the default 240 -491 Adv. UNIX: io/9 4

Character Strings v char name[SIZE], ch; : printf(“Hi, there, %sn”, name); printf(“The letter is

Character Strings v char name[SIZE], ch; : printf(“Hi, there, %sn”, 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 v Specifier Meaning useful for printing unusual chars, see

Less Used printf() Format Specifiers v Specifier Meaning useful for printing unusual chars, see man ascii 240 -491 Adv. UNIX: io/9 %% % character %u Decimal unsigned integer %o Octal integer %x Hexadecimal integer %p Pointer (decimal or hex) %n Store the number of chars written so far continued 6

Examples v int i = 125, num; int *p = &i; upper case hex

Examples v int i = 125, num; int *p = &i; upper case hex letters printf(“%d, %o, %x, %X, %p%nn”, i, i, p, &num); printf(“num = %dn”, num); 125, 175, 7 d, 7 D, 7 fffbe 48 num = 31 240 -491 Adv. UNIX: io/9 the decimal is converted 7

Four Types of Format Modifiers. 1 Flags – e. g. "%+d" printf(". . .

Four Types of Format Modifiers. 1 Flags – e. g. "%+d" printf(". . . ", x, y, z; ( format string . 2 Field Widths – e. g. "%10 d" . 3 Precision – e. g. "%. 4 d" . 4 Type Modifiers – e. g. "%ld" 240 -491 Adv. UNIX: io/9 8

. 2. 1 Flags v Flag Meaning + Prefix positive numbers with a +

. 2. 1 Flags v 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 v printf(“%+dn”, 786, -786); +786 -786 v printf(“% dn”, 547, -547); 547 -547

Examples v printf(“%+dn”, 786, -786); +786 -786 v printf(“% dn”, 547, -547); 547 -547 v printf(“%+09 dn%09 dn”, 452); +00000452 000000452 240 -491 Adv. UNIX: io/9 useful for tidy columns 10

Meaning of# v Prefix 0 is added to octal output (%#o. ( v Prefix

Meaning of# v Prefix 0 is added to octal output (%#o. ( v Prefix 0 x or 0 X is added to hexadecimal output (%#x or %#X. ( v #%e, %#f, %#g have a decimal point, even when there are no digits after it. v #%g does not remove trailing zeros. 240 -491 Adv. UNIX: io/9 11

#Examples v int c = 1427; float p = 1427. 0; printf(“%#o, %#x, %#X,

#Examples v int c = 1427; float p = 1427. 0; printf(“%#o, %#x, %#X, %g, %#gn”, c, c, c, p, p); 02623, 0 x 593, 0 X 593, 1427. 00 octal 240 -491 Adv. UNIX: io/9 hex remove'. ' and 0's do not remove'. ' and 0's 12

. 2. 2 Field Widths v Specify minimum number of characters printed: – if

. 2. 2 Field Widths v 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(“%10

Example = -left justify char *s = “hello”; width int x = 23; printf(“%10 d%10 sn”, x, s); printf(“%-10 d%-10 sn”, x, s); 10 23 240 -491 Adv. UNIX: io/9 10 23 hello 14

Field Width (*) in printf() v Calculate field width based on the next printf()

Field Width (*) in printf() v Calculate field width based on the next printf() argument: #define FIELD_WIDTH 10 printf(“%*dn”, FIELD_WIDTH, val; ( uses FIELD_WIDTH 240 -491 Adv. UNIX: io/9 15

. 2. 3 Precision (. ) in printf() v Specifier Effect %d Min. no.

. 2. 3 Precision (. ) in printf() v 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

Example int i = 873; float f = 123. 94536; char s[] = “Happy Birthday”; printf(“%. 4 dn%. 9 dn”, i, i); printf(“%. 3 fn%. 3 en%. 3 gn”, f, f, f); printf(“%. 11 sn”, s); 0873 000000873 123. 945 1. 293 e+02 124 Happy Birth 240 -491 Adv. UNIX: io/9 17

Meaning of*. * v Calculate field width and precision from the next arguments of

Meaning of*. * v Calculate field width and precision from the next arguments of the printf() printf(“%*. *sn”, field_width, precision, str; ( 240 -491 Adv. UNIX: io/9 18

. 2. 4 Type Modifiers v Place h or l before d, o, x,

. 2. 4 Type Modifiers v Place h or l before d, o, x, or u v Place L before e, f, or g v Letter h l L 240 -491 Adv. UNIX: io/9 Effect Specify short type Specify long double type 19

. 3 Formatted Input: scanf() v 23 2. 3 hi -5 62 int i,

. 3 Formatted Input: scanf() v 23 2. 3 hi -5 62 int i, x, y; float val; char str[100]; scanf(“%d”, scanf(“%f”, scanf(“%s”, &i); &val); str); /* /* /* read in integer */ float */ string */ scanf(“%d%d”, &x, &y); /* read space-separated integers/* 240 -491 Adv. UNIX: io/9 20

Notes v %d will read a sign (e. g. -27( v %f will accept

Notes v %d will read a sign (e. g. -27( v %f will accept a float in fractional or exponential form (e. g. 2. 3, 1. 2 e+02 ( v 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 v %s only reads non-whitespace characters – scanf("%s %s", str 1, str

hello andy v %s only reads non-whitespace characters – scanf("%s %s", str 1, str 2; ( – skips whitespace to start of text – stops reading in text when it gets to a whitespace – adds ‘’ – the string variable (e. g. str 1) must have enough memory 240 -491 Adv. UNIX: io/9 continued 22

scanf("%d%d", &x, &y; ( same as v scanf(“%d”, &x); scanf(“%d”, &y); v 27 13

scanf("%d%d", &x, &y; ( same as v scanf(“%d”, &x); scanf(“%d”, &y); v 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

v %c will read any character (even whitespace: ( scanf(“%c”, &ch; ( v Other

v %c will read any character (even whitespace: ( scanf(“%c”, &ch; ( v 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

v scanf() leaves non-matching characters on the input stream scanf("%d", &x); scanf("%s", str; (

v scanf() leaves non-matching characters on the input stream scanf("%d", &x); scanf("%s", str; ( v hello scanf() returns the number of bound variables, or EOF at end-of-file. int num; num = scanf("%d %d", &x, &y; ( 13 27 240 -491 Adv. UNIX: io/9 continued 25

v scanf() uses line buffering – when scanf() is reading from the keyboard, it

v 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: “);

. 3. 1 Assignment Supression(*) int month, day, year; printf(“Enter date as mm-dd-yy: “); scanf(“%d%*c%d”, &month, &day, &year); printf(“month = %d day = %d year = %dn”, 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 v A field width specifies the maximum number of

. 3. 2 Field Widths v A field width specifies the maximum number of input characters to process (ignoring whitespace before the start. ( v int x, y; scanf(“%2 d%d”, &x, &y); printf(“Integers are: %d, %dn”, x, y); 123456 Integers are: 12, 3456 240 -491 Adv. UNIX: io/9 28

. 3. 3 Scan Sets[. . . ] v Specify characters that can be

. 3. 3 Scan Sets[. . . ] v Specify characters that can be read. v 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[. . . ^] v Specify characters not to be read. v

Inverted Scan Sets[. . . ^] v Specify characters not to be read. v 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 v Things to remember: – scanf() returns when

. 3. 4 Handling Incorrect Input v 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

*/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 v int puts(char *line; ( – e. g. puts(“Hello World;

. 4 Line I/O v 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

v char *gets(char *line; ( – e. g: gets(str; ( Bad style: avoid gets()

v char *gets(char *line; ( – e. g: gets(str; ( Bad style: avoid gets() – Reads to a newline in stdin; replaces newline by ‘’ 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

v char *fgets(char *line, int n, FILE *stream; ( – e. g: fgets(str, 80,

v 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 ‘’ is added – fgets() returns a NULL when EOF or an error occurs. 240 -491 Adv. UNIX: io/9 35

sscanf() v Very useful for separating reading from processing in code. int sscanf(char *s,

sscanf() v Very useful for separating reading from processing in code. int sscanf(char *s, const char *format; (. . . , v 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 v #include <stdio. h> int main() { char str[120]; /* magic

Read then Process v #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 = %dn”, 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 v int putchar(int c; ( – e. g. : v

. 5 Character I/O v int putchar(int c; ( – e. g. : v 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; : {

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 v Quick fix: say “Press RETURN” v 240 -491 Adv. UNIX: io/9 39