210 likes | 219 Views
System Programming. Nittida Nuansri. Streams and File Object. Buffering. Functions Opening a stream Reading & Writing a stream. Standard I/O Efficiency. Streams & File Objects. When we open or create a file with the standard I/O library
E N D
System Programming Nittida Nuansri
Streams and File Object • Buffering • Functions Opening a stream Reading & Writing a stream • Standard I/O Efficiency
Streams & File Objects • When we open or create a file with the standard I/O library • We have associated a stream with the file, such as • fopen() returns a pointer to a FILE object
Streams & File Objects FILE- A structure that contains all information required by the standard I/O library to manage the stream : - The file descriptor used for actual I/O - a pointer to a buffer for the stream - the size of the buffer - a count of the number of characters currently in the buffer - an error flag etc.
Streams & File Objects Application software should never need to examine a FILE object to reference the stream => use a file pointer (type FILE*)
Buffering • Fully buffered • Line buffered • Unbuffered
Buffering Fully buffered - the actual I/O takes place when the standard I/O buffer is filled - flush describes the writing of standard I/O buffer - a buffer can be automatically flushed by the standard I/O routines (such as when a buffer is filled), or a function fflush() is called
Buffering Line buffered - The standard I/O library performs I/O when a new line character is encountered an input or output => It is possible to perform a single character at a time - Typically used a terminal stream (e.g. standard I/O)
Buffering Unbuffered - The standard I/O library does not buffer the characters The standard error stream is normally unbuffered --> so that any error messages are displayed as quickly as possible
Buffering ANSI C By default - standard input and standard output are fully buffered, if and only if they do not refer to an interactive device - standard error is never fully buffered
Buffering • To buffer I/O setbuf (FILE *fp, char *buf); setvbuf (FILE *fp, cahr *buf, int mode, size_t size); where mode is one of the following _IOFBF _IOLBF -IONBF
Buffering • Opening a Stream FILE *fopen(char *pathname, char *type); FILE *freopen(char *pathname, char *type, FILE *fp); FILE *fdopen(int filedes, char *type); return : file pointer if OK, NULL on error type: r, w, a, r+, w+, a+, etc. int fclose (FILE *fp);
Buffering • Standard I/O Library • handles details such as buffer allocation • performing I/O in optimal-sized chunks (users do not need to worry about the correct clock size)
Standard I/O Library • ANSI C -- by default • 1. Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device • 2. Standard error is never fully buffered • Both SVR4 and 4.3+BSD -- defualt • 1. Standard error is always unbuffered • 2. All other streams are line buffered if they refer to a terminal device, otherwise they are fully buffered
Standard I/O Library • The defaults can be altered using void setbuf(FILE *fp, char *buf); int setvbuf(FILE *fp, char *buf, int mode, size_t size); mode : _IOFBF. _IOLBF, _IONBF
Standard I/O Library • Opening a Stream FILE *fopen(char *pathname, char *type); FILE *freopen(char *pathname, char *type, FILE *fp); FILE *fdopen(int filedes, char *type); All three return: file pointer if ok, NULL on error type: r, w, a, r+, w+, a+ int fclose(FILE *fp);
Standard I/O Library • Reading and Writing a Stream • Character-at-a-time • Line-at-a-time • Direct I/O
Standard I/O Library • Functions: • Character-at-a-time: int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void); int putc(FILE *fp); int fputc(FILE *fp); int putchar(void);
Standard I/O Library • Functions: • Character-at-a-time: char *fgets(char *buf, int n, FILE *fp); char *gets(char *buf); char *fputs(char *buf, int n, FILE *fp); char *puts(char *buf);
Standard I/O Library • Assignment : Modify your previous program (about buffer variation test) - use buffer size which yields best performance only - add the following functions fgets, fputs, getc, putc, fgetc, fputc - compare each performance (result from running your program)
Standard I/O Library • Assigment (Cont.) Time Bytes of Function |User System Real prog text -------------------------------------------- best time from | previous program | fgets, fputs | getc, putc | fgets, fputc | single byte time from | previous program |