CSC320 chapter6 - Standard I/O library Defined by Dennis...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Standard I/O library Defined by Dennis Ritchie in 1975 An ANSI C standard Easy to use and portable Details handled: Buffer allocation, optimal-sized I/O portions, better interface, etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Buffering in Standard I/O Standard I/O functions use buffered I/O to reduce the number of system calls. Write(1, buf, 6) /* all output streams are flushed */ /* fflush causes any unwritten data for the stream to be passed to the kernel */
Background image of page 2
3 Buffering in Standard I/O, cont. Goal Use the minimum number of read and write calls. Types 1) 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. flush: standard I/O lib vs terminal driver
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Buffering in Standard I/O, cont. 2) Line Buffered Perform I/O when a newline char is encountered! usually for terminals. Warnings The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested . 3) Unbuffered Expect to output asap, e.g. using write() e.g., stderr
Background image of page 4
5 b ANSI C Requirements Fully buffered for stdin and stdout. Standard error is never fully buffered. #include <stdio.h> int fflush(FILE *fp); All output streams are flushed if fp == NULL Buffering in Standard I/O, cont.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 #include <stdio.h> void setbuf(FILE *fp, char *buf); Full/line buffering (terminal) if buf is not NULL (BUFSIZ) Turn buffereing on or off. To enable buffering, buf must point to a buffer of length BUFSIZ, defined in <stdio.h> #define BUFSIZ 1024 (<stdio.h>) void setvbuf(FILE *fp, char *buf, int mode, size_t size); mode: _IOFBF: Fully Buffered _IOLBF: Line Buffered _IONBF: Unbuffered (the buf and size arguments are ignored) They must be called before any operation is performed on the streams! Buffering in Standard I/O, cont.
Background image of page 6
7 mode buf len type setbuf non-null BUFSIZ FB/LB NULL NB setvbuf FB non-null any size FB FB NULL st_blksize FB LB non-null any size LB LB NULL st_blksize LB NB ignored no buffered NB Buffering in Standard I/O, cont.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 UNIX I/O vs Standard I/O
Background image of page 8
9 UNIX I/O vs Standard I/O, cont. Each system call has analogous procedure calls from the standard I/O library: System call : Standard I/O call : open fopen close fclose read/write getchar/putchar getc/putc fgetc/fputc fread/fwrite gets/puts fgets/fputs scanf/printf fscanf/fprintf lseek fseek
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UNIX I/O vs Standard I/O, cont. Difference from File I/O s File Pointers vs File Descriptors; fopen vs open - When a file is opened/created, a stream is associated with the file. - FILE object File descriptor, buffer size, # of remaining chars, an error flag . ..
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

CSC320 chapter6 - Standard I/O library Defined by Dennis...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online