7 I:O and Pointers

7 I:O and Pointers - !"!"#$ CMSC 216 Introduction to...

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

View Full Document Right Arrow Icon
!"!!"## # CMSC 216 Introduction to Computer Systems Lecture 7 Input/Output & Pointers Jan Plane & Pete Keleher {jplane, [email protected] Administrivia • Project 1 will be graded after late deadline – to return before next project due • Project 2 posted! – Due Feb 28 • Read Reek, Chapter 6: Pointers 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
!"!!"## ! I NPUT /O UTPUT Chapter 15, Reek 3 Formatted I/O • Uses the scanf() and printf() family of functions • Can operate on: – standard input/output – other file streams – strings 4
Background image of page 2
!"!!"## % scanf() family int fscanf(FILE *stream, char *fmt, . ..); – reads formatted input from stream int scanf(char *fmt, . ..); – reads formatted input from stdin int sscanf(char str[], char *fmt, . ..); – reads formatted input from the string str 5 scanf() family • Format strings can contain: – whitespace, meaning any whitespace at that point will be skipped • many (but not all) skip leading whitespace anyway – format specifiers, which cause something to be read – any other characters, which are then required in input scanf() does not check for type agreement between the format specifier and the associated variable • If a given format specifier doesn't match, scanf () stops processing there 6
Background image of page 3

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

View Full DocumentRight Arrow Icon
!"!!"## scanf() format specifiers %d , %x , %o : read a decimal, hex, or octal number into an int %f : read in a float %lf : read in a double %ld : read in a long %c : read in a char %s : read in a string (bounded by whitespace) – When using %s , the array parameter does not use : char str[1024]; scanf("%s", str); 7 Reading data from strings • Used in combination with fgets() , you can ensure that you read data line-by-line, instead of simply treating newlines as whitespace • For what inputs would this loop stop? char buf[1025]; int a, b; while (fgets(buf, 1024, stdin) != NULL) { if (sscanf(buf, "%d %d", &a, &b) == 2) { printf("%d %d\n", a, b); break; } } 8
Background image of page 4
!"!!"## ' The printf() family int fprintf(FILE *stream, char *fmt, . ..); – sends formatted output to stream int printf(char *fmt, . ..); – sends formatted output to stdout int sprintf(char *buf, char *fmt, . ..); – formats output and places in buf , adding null byte int snprintf(char *buf, size_t limit, char *fmt, . ..); – writes at most limit - 1 characters to buf , then null byte 9 Some common format specifiers %c print the corresponding argument to printf as an unsigned character %d print as a decimal integer %u print as an unsigned integer %x print in hexadecimal (use %X for capital A-F) %f print in floating point format %e print in exponential form (e.g., 6.02300e3) %s print as a string (null-terminated character array) %% print a % (so %% prints as % ) 10
Background image of page 5

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

View Full DocumentRight Arrow Icon
!"!!"## ( Controlling formatting • We can supply a field width, precision, and other flags to format our output exactly as we
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 18

7 I:O and Pointers - !"!"#$ CMSC 216 Introduction to...

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

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