CptS 360 (System Programming) Unit 7: The Standard I/O Library Bob - - PowerPoint PPT Presentation

cpts 360 system programming unit 7 the standard i o
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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)

slide-2
SLIDE 2

Unit 7: The Standard I/O Library

Motivation

◮ stdio is the preferred C input/output library. ◮ In most cases, it has optimal I/O efficiency. ◮ It is a good example of an optimized wrapper for low-level

functionality.

◮ Understanding formats allows you to efficiently create output

that matches specifications.

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-3
SLIDE 3

Unit 7: The Standard I/O Library

Reference

◮ Stevens & Rago Ch. 5

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-4
SLIDE 4

Unit 7: The Standard I/O Library

Streams and FILE Objects

◮ use pointers to “FILE” objects instead of small int file

“handles”

◮ FILE *s predefined (in stdio.h):

◮ stdin: standard input ◮ stdout: standard output ◮ stderr: standard error Bob Lewis WSU CptS 360 (Spring, 2020)

slide-5
SLIDE 5

Unit 7: The Standard I/O Library

Buffering

◮ Note: Buffering happens in user mode. ◮ 3 kinds:

◮ fully buffered

buffer written when buffer is full or when fflush(3) is called

◮ line buffered

buffer written either under fully-buffered conditions or when newline written

◮ unbuffered

buffer written when I/O request made (slow!)

◮ controlled by

◮ setbuf(3) ◮ setvbuf(3)

Note: These are necessary but not sufficent for character-at-a-time I/O from a console.

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-6
SLIDE 6

Unit 7: The Standard I/O Library

Opening a Stream

◮ fopen(3)

usual way to open a file for stdio

◮ freopen(3)

allows process to change its standard input (or other FILE *)

◮ fdopen(3)

allows you to perform stdio on an open file descriptor

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-7
SLIDE 7

Unit 7: The Standard I/O Library

Reading and Writing a Stream

◮ getc(3) (or function fgetc(3))

◮ Usually use the macro (getc()).

(Q: Why might you need the function?)

◮ -1 returned for errors or EOF

Call feof(3) to tell the difference.

◮ returns an int, with good reason

◮ getchar() is getc(stdin) ◮ ungetc(3)

pushes back character(s) onto input stream

◮ guaranteed at least one character pushback, even on pipes or

consoles

◮ useful for reading tokens in parsing, e.g., “yyy=xxx3+”

◮ putc(3) (or function fputc(3))

writes a character to a FILE *

◮ putchar(c) is putc(c, stdout)

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-8
SLIDE 8

Unit 7: The Standard I/O Library

Line-at-a-Time Input

◮ fgets(3)

◮ argument n must be ≤ the length of buf. ◮ reads at most n-1 characters. ◮ contrast calling sequence with that of read(2)

◮ gets(3)

◮ FORBIDDEN! DO NOT USE! AIEEEE! ◮ classic example of bad software design

◮ getline(3)

◮ GNU only ◮ optionally allocates the buffer (if so, you must free() it)

◮ readline(3)

◮ GNU only ◮ fancy, customizeable line editing ◮ prompts ◮ works on the terminal Bob Lewis WSU CptS 360 (Spring, 2020)

slide-9
SLIDE 9

Unit 7: The Standard I/O Library

Line-at-a-Time Output

◮ fputs(3)

standard line output

◮ puts(3)

like fputs(str, stdout), but follows it with a newline. Best advice: stick to fgets(3) (or maybe getline(3)) and fputs(3).

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-10
SLIDE 10

Unit 7: The Standard I/O Library

Standard I/O Efficiency

◮ because it’s a standard, stdio has been optimized for your

platform

◮ general rule for applications:

Depart from using stdio only if you know what you’re doing.

◮ Study it as a model to wrap low-level functionality in a

portable, efficient form.

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-11
SLIDE 11

Unit 7: The Standard I/O Library

“Binary” I/O

◮ Q: Why the quotes? ◮ especially handy for reading and writing structs

Aside: What does C++ do when you try to write a class?

◮ fread(3)

◮ Standard invocation to read an n-element array a on fp:

fread(a, sizeof(a[0]), n, fp);

◮ Standard invocation to read a struct s on fp:

fread(&s, sizeof(s), 1, fp);

◮ fwrite(3)

◮ Standard invocation to write an n-element array a on fp:

fwrite(a, sizeof(a[0]), n, fp);

◮ Standard invocation to write a struct s on fp:

fwrite(&s, sizeof(s), 1, fp);

◮ If successful, these return number of items (n) – not bytes –

read or written. (Contrast with read(2) or write(2).)

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-12
SLIDE 12

Unit 7: The Standard I/O Library

Thought Assignment

How would you go about reading the entire contents of a file into a string? Reliably?

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-13
SLIDE 13

Unit 7: The Standard I/O Library

Three Ways to Position a Stream

◮ old reliables:

◮ fseek(3) ◮ ftell(3)

The offset for these is a long, which may be 32 or 64 bits according to the compiler.

◮ new guys:

◮ fseeko(3) ◮ ftello(3)

The offset for these is an off_t, which may be 32 or 64 bits depending on whether or not you #define _FILE_OFFSET_BITS 64 before you include stdio.h.

◮ more portable, but probably more so than you need:

◮ fgetpos(3) ◮ fsetpos(3) Bob Lewis WSU CptS 360 (Spring, 2020)

slide-14
SLIDE 14

Unit 7: The Standard I/O Library

Formatting Output in Standard I/O

You know these:

◮ printf(3) ◮ fprintf(3)

◮ identical to printf(3), but goes to a given FILE *. ◮ In particular, use “fprintf(stderr, ...)” to write to

stderr.

You can use fprintf() to write your own eprintf() to standard error, but you’ll need to study varargs (which we aren’t covering).

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-15
SLIDE 15

Unit 7: The Standard I/O Library

Output Format Specifier

% [option] [width] [.prec] [length] char

◮ note the “metanotation”:

◮ bracketed parts are optional ◮ spaces are there for readability

◮ syntax similar for input and output, but not identical ◮ everything’s optional except “%” and “char ” ◮ we’ll discuss these field-by-field...

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-16
SLIDE 16

Unit 7: The Standard I/O Library

Output Format Syntax Diagram

start '%' flag flag digit digit '.' digit digit lengthmod conversion Bob Lewis WSU CptS 360 (Spring, 2020)

slide-17
SLIDE 17

Unit 7: The Standard I/O Library

The option Field

symbol meaning # alternate form zero padding

  • (negative width) left-justified in field

space blank left before positive number + indicate positive numbers with a leading “+”

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-18
SLIDE 18

Unit 7: The Standard I/O Library

The width Field

symbol meaning decimalNumber minimum field width

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-19
SLIDE 19

Unit 7: The Standard I/O Library

The prec Field

This is a decimal number whose semantics depend on char : char (s) interpretation diouxX minimum number of digits eEf number of digits after decimal point gG maximum number of significant digits s maximum number of characters

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-20
SLIDE 20

Unit 7: The Standard I/O Library

The length Field

This is only meaningful for these char (s): symbol char (s) corresponding argument type is hh diouxX char or unsigned char n char * or unsigned char * h diouxX short int or unsigned short int n short int * or unsigned short int * l diouxX long int or unsigned long int n long int * or unsigned long int * ll diouxX long long int or unsigned long long int n long int * or unsigned long long int * L aAeEfFgG long double Special Purpose: j diouxX intmax_t or uintmax_t z diouxX size_t or ssize_t t diouxX ptrdiff_t

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-21
SLIDE 21

Unit 7: The Standard I/O Library

The char Field: Integer Conversions

char conversion d signed decimal i signed decimal

  • unsigned octal

u unsigned decimal x unsigned hexadecimal (abcdef) X unsigned hexadecimal (ABCDEF)

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-22
SLIDE 22

Unit 7: The Standard I/O Library

The char Field: Floating Point Conversions

char conversion e double is rounded and converted to floating point decimal in exponential notation as spec- fied, always with “e” preceding the exponent E as “e” but exponent precented by “E” f floating point decimal, in lower or mixed case when necessary (e.g. “inf” or “NaN”) F as “f” but output is always upper case (e.g. “INF”) g uses “f” or “e” as needed to provide indicated prec G as “g” but output is always upper case a as “f”, but hexadecimal A as “a” but output is always upper case

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-23
SLIDE 23

Unit 7: The Standard I/O Library

The char Field: Other Conversions

char conversion applied: c unsigned char (also supports wide characters – see man page) s char * (null-terminated string) p void * pointer (as hexadecimal) n (no output, but number of characters written so far stored in corresponding int pointer) % % (escaped “%” in format string)

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-24
SLIDE 24

Unit 7: The Standard I/O Library

In-Memory Formatting

These handy functions perform printf(3)-like conversions into a char buffer (1st argument):

◮ sprintf(3)

slightly insecure. When in doubt, use one of the others.

◮ snprint(3)

limit buffer to n bytes (including null terminator)

◮ asprintf(3)

allocate (on the heap) a large-enough buffer (which you must free(3))

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-25
SLIDE 25

Unit 7: The Standard I/O Library

Additional Notes on sprintf(3)-style Formatting

◮ Fields are never truncated. ◮ “*” allows an int variable to replace either width or precision. ◮ v* variants (e.g. vsprintf(3)) allow for “varargs” (q.v.)

functions.

◮ For security, use snprintf(3), which limits the size of the

buffer, instead of sprintf(3).

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-26
SLIDE 26

Unit 7: The Standard I/O Library

Test Yourself

What are the most likely types of the vars and what do these statements output?

a) printf("%4d", var); b) printf("%07d", var); c) printf("%-8s", var); d) printf("%*s", var0, var1); e) printf("%f%%", var); f) printf("0x%08x", var); g) printf("%u", var); h) printf("%lli", var); i) printf("%10.3f", var); j) printf("%10.3g", var); k) printf("%Le", var);

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-27
SLIDE 27

Unit 7: The Standard I/O Library

Standard Input

◮ scanf(3)

reads and converts from standard input

◮ fscanf(3)

reads and converts from an arbitrary “FILE *”

◮ sscanf(3)

reads and converts from a char buffer Watch buffer sizes!

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-28
SLIDE 28

Unit 7: The Standard I/O Library

Input Format Specification

% [ *] [ a] [maxWidth] [typeMod] char

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-29
SLIDE 29

Unit 7: The Standard I/O Library

scanf(3) Fields I

field char means * any suppress assignment (matches, but skips datum) a cs malloc(3) appropriate buffer (not in ANSI C, unfortunately). Argument must be char **. User must free(3) it when done with it. maxWidth any decimal integer indicating the maximum field width to be converted (null termi- nator, if any, is not included) hh diouxX short char * coming h diouxX unsigned short int * coming l diouxX long int * coming l eEfga double * coming

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-30
SLIDE 30

Unit 7: The Standard I/O Library

scanf(3) Fields II

field char means L diouxX long long int * coming L eEfga long double * coming Special Purpose: j diouxX intmax_t * or uintmax_t * coming z diouxX size_t * or ssize_t * t diouxX ptrdiff_t *

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-31
SLIDE 31

Unit 7: The Standard I/O Library

scanf(3) Integer Conversions

char means % matches literal % d (signed) decimal integer D

FORBIDDEN! DO NOT USE! AIEEEE!

i (signed) decimal integer

  • unsigned octal integer

u unsigned decimal x unsigned hexadecimal X unsigned hexadecimal

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-32
SLIDE 32

Unit 7: The Standard I/O Library

scanf(3) Float and Other Conversions

char means eEfg floating point (float, double,

  • r long double)

s sequence

  • f

non-whitespace characters c sequence of maxWidth (default: 1) characters [sequenceOfCharacters ] any

  • ne
  • f

sequenceOfCharacters p pointer to void * n store number of characters con- sumed in pointer

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-33
SLIDE 33

Unit 7: The Standard I/O Library

Temporary Files

When you need to create a temporary intermediate file, choices are:

◮ tmpnam(3)

FORBIDDEN! DO NOT USE! AIEEEE!

◮ mkstemp(3)

Better: It creates an unique file and returns a file descriptor for it.

◮ tmpfile(3)

Maybe even better: It creates an unique temporary file, fopen()s it, and returns its FILE *. In addition, the file will go away when the program exits (or it’s fclose()d).

Bob Lewis WSU CptS 360 (Spring, 2020)