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 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)
Unit 7: The Standard I/O Library Reference ◮ Stevens & Rago Ch. 5 Bob Lewis WSU CptS 360 (Spring, 2020)
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)
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)
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)
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=xxx 3+” ◮ 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)
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)
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)
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)
Unit 7: The Standard I/O Library “Binary” I/O ◮ Q: Why the quotes? ◮ especially handy for reading and writing struct s 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)
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)
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)
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)
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)
Unit 7: The Standard I/O Library Output Format Syntax Diagram digit flag digit digit digit flag '.' lengthmod '%' start conversion Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library The option Field symbol meaning alternate form # 0 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)
Unit 7: The Standard I/O Library The width Field symbol meaning decimalNumber minimum field width Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library The prec Field This is a decimal number whose semantics depend on char : char (s) interpretation minimum number of digits diouxX eEf number of digits after decimal point maximum number of significant digits gG s maximum number of characters Bob Lewis WSU CptS 360 (Spring, 2020)
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 char * or unsigned char * n h diouxX short int or unsigned short int short int * or unsigned short int * n l diouxX long int or unsigned long int long int * or unsigned long int * n ll diouxX long long int or unsigned long long int long int * or unsigned long long int * n L aAeEfFgG long double Special Purpose: intmax_t or uintmax_t j diouxX z diouxX size_t or ssize_t t diouxX ptrdiff_t Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library The char Field: Integer Conversions char conversion d signed decimal signed decimal i o unsigned octal unsigned decimal u unsigned hexadecimal ( abcdef ) x unsigned hexadecimal ( ABCDEF ) X Bob Lewis WSU CptS 360 (Spring, 2020)
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 as “ e ” but exponent precented by “ E ” E floating point decimal, in lower or mixed case f when necessary (e.g. “ inf ” or “ NaN ”) as “ f ” but output is always upper case (e.g. F “ INF ”) uses “ f ” or “ e ” as needed to provide indicated g prec as “ g ” but output is always upper case G a as “ f ”, but hexadecimal as “ a ” but output is always upper case A Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 7: The Standard I/O Library The char Field: Other Conversions conversion applied: char unsigned char (also supports wide characters c – see man page) char * (null-terminated string) s void * pointer (as hexadecimal) p (no output, but number of characters written so n far stored in corresponding int pointer) % % (escaped “ % ” in format string) Bob Lewis WSU CptS 360 (Spring, 2020)
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)
Recommend
More recommend