Stream Model of IO Basic IO in C

Stream Model of I/O Basic I/O in C 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object is simply a serialized view of that other object. input file To be, or not to be? T o b e o r . . . That is the question. executing process We think of data as flowing in the stream to the process, which can remove data from the stream as desired. The data in the stream cannot be lost by “flowing past” before the program has a chance to remove it. header file: <stdio. h> CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

C FILE Type Basic I/O in C 2 Accessing a stream requires a pointer of type FILE. C provides three standard streams, which require no special preparation other than the necessary include directive: stdin standard input keyboard stdout standard output console window stderr standard error console window Alternatively, you can declare FILE pointers and manage their connections to disk files. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Output with printf() Basic I/O in C 3 The Standard Library provides the printf() function which can be used to write formatted output to the standard output stream, stdout. int printf(const char * restrict format, . . . ); The first parameter is a string literal that specifies the formatting to be used. The remaining parameters, if any, specify the values to be written. The return value is the number of characters that were written, or a negative value if an error was encountered. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Output Examples Basic I/O in C 4 The specification of format is mildly complex: int i = 42, j = 17; double x = 3. 1415962; printf("i =%3 d j =%3 dn", i, j); printf("x =%7. 4 fn", x); i = 42 j = 17 x = 3. 1416 There is an excellent discussion of the details in section 22. 3 of the King book. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

More on printf() Format Specifiers flags: CS@VT August 2009 m co od sp nv ifie r ec er s ifi io er n le ng th on si pr ec i w in m % fla gs id th The general form of a printf() specifier is: Basic I/O in C 5 #0 12 . 5 L g optional, more than one allowed - left-justify within field + always show leading sign space precede non-negative numbers with a space # see reference 0 pad with zeros to fill field Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

More on printf() Format Specifiers m co od sp nv ifie r ec er s ifi io er n le ng th on si pr ec i w in m % fla gs id th The general form of a printf() specifier is: Basic I/O in C 6 #0 12 . 5 L g min width: optional, pad with spaces if field not filled, ignored if insufficient precision: optional, number of digits for integer values, number of digits after decimal point for float/double CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

More on printf() Format Specifiers m co od sp nv ifie r ec er s ifi io er n le ng th on si pr ec i w in m % fla gs id th The general form of a printf() specifier is: Basic I/O in C 7 #0 12 . 5 L g length modifier: optional, indicates value has a type that's longer or shorter than is normal for a particular conversion specifier (see following) d normally used for int values u normally used for unsigned int values hd normally used for short int values ld normally used for long int values see reference for more… Table 22. 5 CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

More on printf() Format Specifiers m co od sp nv ifie r ec er s ifi io er n le ng th on si pr ec i w in m % fla gs id th The general form of a printf() specifier is: Basic I/O in C 8 #0 12 . 5 L g conversion specifier: mandatory d, i converts int to decimal form f, F converts double to decimal form; default precison 6 c displays int as unsigned character see reference for more… Table 22. 6 CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Format Specifiers for <stdint. h> Basic I/O in C 9 The basic integer format codes will work with int 32_t and uint 32_t (with compiler warnings), but are not reliable with int 64_t and uint 64_t. The header file <inttypes. h> provides specialized format codes for the new integer types. Here's a very brief description; see King 27. 2 for details. PRId. N for signed integer types, N = 8, 16, 32, 64 PRIu. N for unsigned integer types For example: uint 64_t K = 123456789012345; printf("%15"PRIu 64"n", K); CS@VT August 2009 // note use of quotes!! Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Input with scanf() Basic I/O in C 10 The Standard Library provides the scanf() function which can be used to write formatted output to the standard output stream, stdin. int scanf(const char * restrict format, . . . ); The first parameter is a string literal that specifies the formatting expected in the input stream. The remaining parameters, if any, specify the variables that will receive the values that are read. The return value is the number of values that were read, or the value of EOF if an input failure occurs. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Input Examples Basic I/O in C 11 Suppose we have an input stream of the following form: 17 42 3. 14159625 17 42 3. 1416 int i = 1, j = 1; double x = 1. 5; scanf("%d %d %f", &i, &j, &x); printf("%5 d %7. 4 fn", i, j, x); Suppose we have an input stream of the following form: 17, 42, 3. 14159625 int i = 1, j = 1; double x = 1. 5; scanf("%d, %f", &i, &j, &x); printf("%5 d %7. 4 fn", i, j, x); 17 CS@VT August 2009 Computer Organization I 42 3. 1416 © 2006 -09 Mc. Quain, Feng & Ribbens

* m ng th le % m ax w id th The general form of a scanf() specifier is: 12 L g *: optional, read but do not assign value to an object max width: optional, leading whitespace doesn't count CS@VT August 2009 Basic I/O in C 12 od co ifi sp nv er ec er ifi sio er n More on scanf() Format Specifiers Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

* m ng th le % m ax w id th The general form of a scanf() specifier is: 12 L Basic I/O in C 13 od co ifi sp nv er ec er ifi sio er n More on scanf() Format Specifiers g length modifier: optional, indicates object that will receive value has a type that's longer or shorter than is normal for a particular conversion specifier (see following) see reference for more… Table 22. 11 conversion specifier: mandatory see reference for more… Table 22. 12 CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Opening Files Basic I/O in C 14 File I/O is almost identical to I/O with stdin and stdout. You must make a call in order to associate a FILE pointer with a particular file: FILE *fopen(const char* restrict filename, const char* restrict mode); filename mode path/name of file to be opened "r" "w" "a" "rb" "wb" "ab" "r+" "w+" "a+" see reference for more Return value is valid FILE pointer if successful and NULL otherwise. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

File I/O Basic I/O in C 15 File I/O is accomplished using variations of printf() and scanf(): int fprintf(FILE * restrict stream, const char * restrict format, . . . ); int fscanf(FILE * restrict stream, const char * restrict format, . . . ); These are used in the same way as their counterparts, aside from taking a FILE*. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Closing Files Basic I/O in C 16 When done with a file, you should close the file: int fclose(FILE *stream); Any unwritten buffered data will be delivered to the host environment. Any unread buffered data will be discarded. Returns 0 on success and EOF otherwise. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens
- Slides: 16