260 likes | 447 Views
Standard I/O Library. ANSI C standard specification handles details of I/O operations buffering block sizing uses calls to lower level functions. Streams and File Objects. streams associated with files via file object FP pointer to a file object used for stream management
E N D
Standard I/O Library ANSI C standard specification handles details of I/O operations buffering block sizing uses calls to lower level functions
Streams and File Objects • streams associated with files via file object • FP pointer to a file object used for stream management • object ‘black box’ to programmer • contains relevant info for file • FP’s passed to I/O functions for operations
stdin, stdout, stderr • three file pointers that reference the same files as file descriptors STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO • use FP’s stdin, stdout, stderr from stdio.h
Buffering • used to decrease read/write call overhead • intended to be as automatic as possible • three types of buffering • fully buffered • line buffered • unbuffered • buffer obtained at first I/O function using open stream • background operation • buffering can be specified with setbuf, setvbuf
Fully Buffered I/O • actual I/O takes place when buffer is full • buffer is flushed when full, or when manually flushed via fflush. • ANSI C required IF not interactive
Line Buffered I/O • actual I/O takes place when newline character is encountered, or buffer is full, or input is requested via an interactive device • line buffering typically used on terminals • since I/O takes place when full, does not necessarily imply ‘line’ ouput
Unbuffered I/O • actual I/O takes place immediately, without buffering • standard error normally unbuffered • ANSI C requirement
Opening a Stream • fopen, freopen, fdopen functions for opening streams • All return FP on success, NULL on error • All require type specification
fopen • opens a specified file
freopen • opens file on specified stream • closes stream first if already open • typically used to open file on standard stream
fdopen • associates existing FD with stream (FP) • uses previously obtained FD from low-level call (open,fcntl, etc.) • typically used with network com channels
Stream opening types • read • write • append • read and write • Each type has several extended capabilities • binary option possible on all, but meaningless under UNIX
Closing a stream • Streams are closed using fclose • flushes output buffer and discards input buffer before close • normal program termination will flush and close streams
Reading and Writing a Stream • Three types of unformatted I/O • character • one character at a time • line • one line at a time • direct (object) • one object at a time • sometimes called binary, structure or record I/O
Input Functions • getc (macro) • takes FP • fgetc (true function) • takes FP • getchar • getc(stdin) • all used for single character input
Handling EOF • functions return same value for EOF or error • ferror, feof, clearerr used to handle EOF • take FP, return TRUE/FALSE • clearerr clears EOF and error flags for FP
Push back • ungetc used to push characters back • takes # of chars, FP • assume only one char possible • once pushed back, character will be read on next read call • used for ‘peeking’ at data
Output Functions • putc (macro) • fputc (true function) • putchar • putc(c, stdout) • all used for single character output
I/O by lines • use fgets, gets for input • gets deprecated (DO NOT USE) • no buffer size specification • drops newline • use fputs, puts for output • puts add newline • use for line (string) input, output • buffers are automatically terminated with NULL
Binary I/O • use fread, fwrite • used for reading/writing structures • replace getc, putc using loops • line I/O not usable for embedded newlines, NULLS
Positioning a Stream • use functions ftell, fseek, fgetpos, fsetpos • ftell fseek UNIX specific (long integer) • fgetpos, fsetpos portable • use fpos_t for position
Formatted Output • use printf, fprintf, sprintf • printf writes to stdout • fprintf writes to FP • sprintf fills an array • appends NULL • ‘vprint...’ functions identical, but used with variable argument lists (see stdarg)
Formatted Input • use scanf, fscanf, sscanf • scanf reads from stdin • fscanf reads from FP • sscanf reads from string • vscanf... functions identical but take variable argument list (see stdarg)
Implementation • standard lib functions call low level I/O routines • FD assigned to each FP • obtain with fileno function • stdio.h has all basic definititions for given system
Temporary Files • use tmpnam, tmpfile • used to create temporary files • tmpnam creates (cross your fingers) unique pathname • corresponding file must be created, or static buffer used • mkstemp preferred under Linux • tempnam same as tmpname but allows prefix directory and prefix specification • tmpfile creates binary file