国外C语言程序设计英文课件-lecture13
Lecture 13 Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting I/O to files. Pipes Bibliography: [Kochan, chap 16.1, 16.2] [Kernighan&Ritche, chap 7.1, 7.2] [C Primer, chap 8] Standard Input and Output input and output devices: such as keyboards, disk drives, printers, screen. The C language itself does not have any special statements for performing input/output (I/O) operations; all I/O operations in C must be carried out through function calls. These functions are contained in the standard C library: #include Advantages: Portability: they work in a wide variety of computer environments General character: they generalize to using files for I/O Disadvantage: Less performance: they don't take advantage of features peculiar to a particular system. Kinds of Standard I/O functions Character I/O getchar, putchar Formatted I/O scanf, printf Character I/O example /* echo.c -- repeats input */ #include int main(void) { char ch; while ((ch = getchar() ) != ‘*’) putchar(ch); return 0; } I/O streams C treats input and output devices the same as it treats regular files on storage devices. In particular, the keyboard and the display devices are treated as files opened automatically by every C program. Conceptually, the C program deals with a stream instead of directly with a file. A stream is an idealized flow of data to which the actual input or output is mapped. Keyboard input is represented by a stream called stdin, and output to the screen is represented by a stream called stdout. The getchar(), putchar(), printf(), and scanf() functions are all members of the standard I/O package, and they deal with these two streams. EOF One implication of I/O streams is that you can use the same techniques with keyboard input as you do with files. For example, a program reading a file needs a way to detect the end of the file so that it knows