Unit 7: The Standard I/O Library
CptS 360 (System Programming) Unit 7: The Standard I/O Library
Bob Lewis
School of Engineering and Applied Sciences Washington State University
Spring, 2020
Bob Lewis WSU CptS 360 (Spring, 2020)
CptS 360 (System Programming) Unit 7: The Standard I/O Library Bob - - PowerPoint PPT Presentation
Unit 7: The Standard I/O Library CptS 360 (System Programming) Unit 7: The Standard I/O Library Bob Lewis School of Engineering and Applied Sciences Washington State University Spring, 2020 Bob Lewis WSU CptS 360 (Spring, 2020) Unit 7: The
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ stdin: standard input ◮ stdout: standard output ◮ stderr: standard error Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ fully buffered
◮ line buffered
◮ unbuffered
◮ setbuf(3) ◮ setvbuf(3)
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ Usually use the macro (getc()).
◮ -1 returned for errors or EOF
◮ returns an int, with good reason
◮ guaranteed at least one character pushback, even on pipes or
◮ useful for reading tokens in parsing, e.g., “yyy=xxx3+”
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ argument n must be ≤ the length of buf. ◮ reads at most n-1 characters. ◮ contrast calling sequence with that of read(2)
◮ FORBIDDEN! DO NOT USE! AIEEEE! ◮ classic example of bad software design
◮ GNU only ◮ optionally allocates the buffer (if so, you must free() it)
◮ GNU only ◮ fancy, customizeable line editing ◮ prompts ◮ works on the terminal Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ Standard invocation to read an n-element array a on fp:
◮ Standard invocation to read a struct s on fp:
◮ Standard invocation to write an n-element array a on fp:
◮ Standard invocation to write a struct s on fp:
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ fseek(3) ◮ ftell(3)
◮ fseeko(3) ◮ ftello(3)
◮ fgetpos(3) ◮ fsetpos(3) Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ identical to printf(3), but goes to a given FILE *. ◮ In particular, use “fprintf(stderr, ...)” to write to
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
◮ bracketed parts are optional ◮ spaces are there for readability
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
start '%' flag flag digit digit '.' digit digit lengthmod conversion Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library
Bob Lewis WSU CptS 360 (Spring, 2020)