1、System Programming,Chapter 5 Standard I/O Library,Announcement,Programming assignment #1 3/15/2006 4/7/2006 Bring your labtop PC (if you have one) for your next class Active slides,Fun Video I,Paper Windows (Queens, Canada) vs. Graphical Windows,Fun Video II,I/O Brush: The World as the Palette (MIT
2、Media Lab),Document Your Code,The more comments the better? Well documented: Some code explains itself. The comments are written for the readers to understand the code not to misguide the readers.,What does the following code fragment do?,/ pi is a float, start with 0 float pi = 0; / i is an integer
3、, go from 0 to 10000 for (int i=0; i10000; i+) / initialize the floats x an y with random numbersfloat x = (float)rand() / RAND_MAX;float y = (float)rand() / RAND_MAX; / add .0004 to pi if x*x + y*y is smaller than 1if ( x*x + y*y 1 ) pi += .0004; ,Calculation of Pi Using the Monte Carlo Method,The
4、“Monte Carlo Method“ is a method of solving problems using statistics. The probability of randomly selecting a point (x,y) inside the circle to that inside the square is p/4.,(x, y),Take a 2nd look,/ estimate pi by using a Monte Carlo algorithm: / get 10000 random points in the 01 x/y range and use
5、/ Pythagoras to check / if they are inside the circle. Add 4/10000 for each / point within the circle. float pi = 0; for (int i=0; i10000; i+) float x = (float)rand() / RAND_MAX;float y = (float)rand() / RAND_MAX;if ( x*x + y*y 1 ) pi += .0004; ,Contents,Preface/Introduction Standardization and Impl
6、ementation File I/O Standard I/O Library Files and Directories System Data Files and Information Environment of a Unix Process Process Control Signals Inter-process Communication,Standard I/O Library,A major revision by Dennis Ritchie in 1975 based on the Portable I/O library by Mike Lesk An ANSI C
7、standard Easy to use and portable Details handled: Buffer allocation, optimal-sized I/O chunks, better interface, etc.,Standard I/O Library,Differences from File I/O File Pointers vs. File Descriptors fopen vs open When a file is opened/created, a stream is associated with the file. FILE object File
8、 descriptor, buffer size, # of remaining chars, an error flag, and the like. stdin, sdtout, stderr defined in STDIO_FILENO, STDOUT_FILENO,Unbuffered I/O,read( ),read( ),read( ),User Process,Kernel,Storage,Buffered I/O,gets( ),gets( ),gets( ),User Process,Kernel,Storage,Standard I/O Buffer,Buffering,
9、Goal Use the minimum number of read and write calls. Types Fully Buffered Actual I/O occurs when the buffer is filled up. A buffer is automatically allocated when the first-time I/O is performed on a stream. Meaning of “flush”: standard I/O lib vs terminal driver,Buffering,Line Buffered Perform I/O
10、when a newline char is encountered! usually for terminals. Caveats The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested. Unbuffered Expect to output ASAP, e.g. using write() E.g., stderr,Buffering,ANSI C Requirements Fully buffered for stdi
11、n and stdout unless interactive devices are referred to. SVR4/4.3+BSD line buffered Standard error is never fully buffered.#include int fflush(FILE *fp); All output streams are flushed if fp = NULL,Buffering,#include void setbuf(FILE *fp, char *buf); int setvbuf(FILE *fp, char *buf, int mode, size_t
12、 size); Full/line buffering if buf is not NULL (BUFSIZ) Terminals mode: _IOFBF, IOLBF, _IONBF () #define BUFSIZ 1024 () They must be called before any op is performed on the streams! What if you want to change it after some op?,Buffering,Possible Memory Access Errors Use automatic allocation NULL fo
13、r *buf in setvbuf() bookkeeping,Standard I/O Library - Open,#include FILE *fopen(const char *pathname, const char *type); FILE *freopen(const char *pathname, const char *type, FILE *fp); fopen/freopen opens a specified file! POSIX.1 Close fp stream first! New files created by a or w have r/w rights
14、for all,Standard I/O Library - Open,Type r w a r+ w+ a+ File exists? Y Y Truncate Y Y R Y Y Y Y W Y Y Y Y Y W only at end Y Y,Standard I/O Library - Open,#include FILE *fdopen(int fildes, const char *type); Associate a standard I/O stream with an existing file descriptor POSIX.1 Pipes, network chann
15、els No truncating for the file for “w” b (in rb, wb, ab, r+b, ) stands for a binary file no effect for Unix kernel O_APPEND supports multiple access. Interleaved R&W restrictions intervening fflush (WR), fseek(WR/RW), fsetpos (WR/RW), rewind (WR/RW), EOF (RW),Standard I/O Library Open/Close,#include
16、 int fclose(FILE *fp); Flush buffered output Discard buffered input All I/O streams are closed after the process exits.setbuf or setvbuf to change the buffering of a file before any operation on the stream.,Multi-byte Files,Standard I/O file streams can be used with single byte and multiple byte cha
17、racter sets.Mode: Negative: set byte-oriented Positive: set wide-oriented 0: No change on orientation. Return values: Positive: wide oriented Negative: byte oriented 0: no orientation.,#include #include int fwide(FILE *fp, int mode);,Standard I/O Library Reading/Writing,Unformatted I/O Character-at-
18、a-time I/O, e.g., getc() Buffering handled by standard I/O lib Line-at-a-time I/O, e.g., fgets() Buffer limit might need to be specified. Direct I/O, e.g., fread() Read/write a number of objects of a specified size. An ANSI C term, e.g., = object-at-a-time I/O,Standard I/O Library Reading/Writing,#i
19、nclude int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void); return next character if okay, EOF on end of file or error getchar = getc(stdin) Differences between getc and fgetc getc could be a macro Arguments side effect (e.g., getc(f*+) will fail, exec time, passing of the function address. u
20、nsigned char converted to int in returning,Standard I/O Library Reading/Writing,#include int ferror(FILE *fp); int feof(FILE *fp); void clearerr(FILE *fp); An error flag and an EOF flag for each FILE int ungetc(int c, FILE *fp); Return c if OK, EOF on error No pushing back of EOF (i.e., -1) No need
21、to be the same char read!,Standard I/O Library Reading/Writing,#include int putc(int c, FILE *fp); int fputc(int c, FILE *fp); int putchar(int c); Return c if ok, EOF on error putchar(c) = putc(c, stdout) Differences between putc and fputc putc() can be a macro.,Line-at-a-Time I/O,#include char *fge
22、ts(char *buf, int n, FILE *fp); Include n and be terminated by null Could return a partial line if the line is too long. Return buf if OK, NULL on end of file or error char *gets(char *buf); Read from stdin. No buffer size is specified overflow *buf does not include n and is terminated by null. Retu
23、rn buf if OK, NULL on end of file or error,Line-at-a-Time I/O,#include char *fputs(const char *str, FILE *fp); *str is a null-terminated string No need for line-at-a-time output. Return non-negative value if OK, EOF on error char *puts(const char *str); *str is a null-terminated string puts then wri
24、tes n to stdout. Return non-negative value if OK, EOF on error,Standard I/O Efficiency (98.5 MB & 3M lines),Program 5.1 Page 143 Copy stdin to stdout: getc putc Program 5.2 Page 144 Copy stdin to stdout: fgets fputs,Binary I/O,Objectives Read/write a structure at a time, which could contains null or
25、 n.#include size_t fread(void *ptr, size_t size, size_t nobj, FILE *fp); size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *fp);Return than the specified number of objects error or EOF ferror, feof Write error if less than the specified number of objects are written.,Binary I/O,Example 1
26、 float data10; if (fwrite(,Binary I/O,Not portable for programs using fread and fwrite The offset of a member in a structure can differ between compilers and systems (due to alignment). The binary formats for various data types, such as integers, could be different over different machines.,Positioni
27、ng-a-Stream,#include long ftell(FILE *fp); int fseek(FILE *fp, long offset, int whence); void rewind(FILE *fp); Assumption: a files position can be stored in a long (since Version 7) whence: same as lseek Binary files: No requirements for SEEK_END under ANSI C (good under Unix, possible padding for
28、other systems). Text files: SEEK_SET only 0 or returned value by ftell (different formats for some sys).,Positioning-a-Stream,#include long fgetpos(FILE *fp, fpos_t *pos); int fsetpos(FILE *fp, const fpos_t *pos); ANSI C standard Good for non-Unix systems A new data type fpos_t,Formatted I/O Output,
29、#include int printf(const char *format, ); int fprintf(FILE *fp, const char *format, ); int sprintf(char *buf, const char *format, ); Overflow is possible for sprintf() 0 appended at the end of the string. int vprintf(const char *format, var_list arg); int vfprintf(FILE *fp, const char *format, var_
30、list arg); int vsprintf(char *buf, const char *format, var_list arg);,Formatted I/O Input,#include int scanf(const char *format, ); int fscanf(FILE *fp, const char *format, ); int sscanf(char *buf, const char *format, );,Implementation Details,#include int fileno(FILE *fp); Get filedes for fcntl, du
31、p, etc See for per-stream flags, etc. Program 5.3 Page 154 Printing buffering for various I/O streams stdin, stdout line-buffered, buf size stderr unbuffered, buf size files: fully-buffered, buf size,Temporary Files,#include char *tmpnam(char *ptr); TMP_MAX in /* = 25, ANSI C */ If ptr = null, the p
32、ointer to the pathname is returned (L_tmpnam # of bytes assumed if ptr != null). FILE *tmpfile(void); wb+ an empty binary file. Unlink the file immediately after it is open! Program 5.4 Page 156 tmpnam and tmpfile,Temporary Files,#include char *tempnam(const char *directory, const char *prefix); TMP
33、DIR *directory is null? P_tmpdir in /tmp /* prefix could be up to 5 chars */ Not POSIX.1 and ANSI C, but XPG3 (SVR4, 4.3+BSD) Program 5.5 Page 157 tempnam,Alternatives to Standard I/O,Main Issue Too many data copyings kernel standard I/O buffer standard I/O buffer our buffer Alternatives Fast I/O Library (fio) pointer sfio Represent files/memory regions under I/O streams, and stack processing modules above I/O streams. mmap,