Standard I/O Library - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Standard I/O Library

Description:

streams associated with files via file object. FP pointer to a file object used for ... gets deprecated (DO NOT USE) no buffer size specification. drops newline ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 26
Provided by: richarde78
Category:

less

Transcript and Presenter's Notes

Title: Standard I/O Library


1
Standard I/O Library
  • ANSI C standard specification
  • handles details of I/O operations
  • buffering
  • block sizing
  • uses calls to lower level functions

2
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
  • FPs passed to I/O functions for operations

3
stdin, stdout, stderr
  • three file pointers that reference the same files
    as file descriptors STDIN_FILENO, STDOUT_FILENO,
    STDERR_FILENO
  • use FPs stdin, stdout, stderr from stdio.h

4
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

5
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

6
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

7
Unbuffered I/O
  • actual I/O takes place immediately, without
    buffering
  • standard error normally unbuffered
  • ANSI C requirement

8
Opening a Stream
  • fopen, freopen, fdopen functions for opening
    streams
  • All return FP on success, NULL on error
  • All require type specification

9
fopen
  • opens a specified file

10
freopen
  • opens file on specified stream
  • closes stream first if already open
  • typically used to open file on standard stream

11
fdopen
  • associates existing FD with stream (FP)
  • uses previously obtained FD from low-level call
    (open,fcntl, etc.)
  • typically used with network com channels

12
Stream opening types
  • read
  • write
  • append
  • read and write
  • Each type has several extended capabilities
  • binary option possible on all, but meaningless
    under UNIX

13
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

14
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

15
Input Functions
  • getc (macro)
  • takes FP
  • fgetc (true function)
  • takes FP
  • getchar
  • getc(stdin)
  • all used for single character input

16
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

17
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

18
Output Functions
  • putc (macro)
  • fputc (true function)
  • putchar
  • putc(c, stdout)
  • all used for single character output

19
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

20
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

21
Positioning a Stream
  • use functions ftell, fseek, fgetpos, fsetpos
  • ftell fseek UNIX specific (long integer)
  • fgetpos, fsetpos portable
  • use fpos_t for position

22
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)

23
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)

24
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

25
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
Write a Comment
User Comments (0)
About PowerShow.com