240-491 Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C 240-491 Special Topics in Comp.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 4 Input and Output.
Chapter 18 I/O in C.
1 Pertemuan 4 Fungsi-Fungsi Bahasa C Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
File IO and command line input CSE 2451 Rong Shi.
Dale Roberts Basic I/O (Chap. 9) CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
 Integers and Characters  Character Strings  Input and Output.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
CSE1301 Computer Programming: Lecture 6 Input/Output.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 1704 Introduction to Data Structures and Software Engineering.
Introduction to Programming Lecture 5: Interaction.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Plan for the Day: I/O (beyond scanf and printf)
Plan of the Day: More on type conversions scanf printf format strings
Programming in C Input / Output.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 08- printf and scanf
Formatted Input/Output
CSI 121 Structured Programming Language Lecture 7: Input/Output
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Chapter 18 I/O in C.
Department of Computer and Information Science
Chapter 4 Managing Input and Output Operations
Programming in C Input / Output.
Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
Module 12 Input and Output
Introduction to C EECS May 2019.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Chapter 18 I/O in C.
Presentation transcript:

Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp. Eng. 1 Semester 2, Standard I/O in C

Adv. UNIX:io/92 Overview 1. Background 2. Formatted Output: printf() 3. Formatted Input: scanf() 4. Line I/O: gets(), puts() 5. Character I/O: getchar(), putchar()

Adv. UNIX:io/93 1. Background  Most I/O library functions are in stdio.h v Basic methods: –1. Formatted I/O –2. Line by line –3. Character by character

Adv. UNIX:io/94 2. Formatted Output: printf()  int i = 2, j = 7; printf(“i = %d, j = %d\n”, i, j);  float, double printed with %f, %e, %g : double x = 3.14; printf(“%f, %e, %g\n”, x, x, x); , e+00, dp is the default

Adv. UNIX:io/95 Character Strings v char name[SIZE], ch; : printf(“Hi, there, %s\n”, name); printf(“The letter is %c \n”, ch); See man ascii for some unusual chars, e.g. ' \a' '\b' '\r'

Adv. UNIX:io/96 Less Used printf() Format Specifiers v SpecifierMeaning %% character %uDecimal unsigned integer %oOctal integer %xHexadecimal integer %pPointer (decimal or hex) %nStore the number of chars written so far continued useful for printing unusual chars, see man ascii

Adv. UNIX:io/97 Examples v int i = 125, num; int *p = &i; printf(“%d, %o, %x, %X, %p%n\n”, i, i, i, i, p, &num); printf(“num = %d\n”, num); 125, 175, 7d, 7D, 7fffbe48 num = 31 upper case hex letters the decimal is converted

Adv. UNIX:io/98 Four Types of Format Modifiers 1. Flags –e.g. "%+d" 2. Field Widths –e.g. "%10d" 3. Precision –e.g. "%.4d" 4. Type Modifiers –e.g. "%ld" printf(".....", x, y, z); format string

Adv. UNIX:io/ Flags v FlagMeaning +Prefix positive numbers with a + sign spacePrefix positive numbers with a space 0Pad with leading zeros #Alternate output form

Adv. UNIX:io/910 Examples  printf(“%+d\n%+d\n”, 786, -786);  printf(“% d\n% d\n”, 547, -547); v printf(“%+09d\n%09d\n”, 452, 452); useful for tidy columns

Adv. UNIX:io/911 Meaning of #  Prefix 0 is added to octal output ( %#o ).  Prefix 0x or 0X is added to hexadecimal output ( %#x or %#X ).  %#e, %#f, %#g have a decimal point, even when there are no digits after it.  %#g does not remove trailing zeros.

Adv. UNIX:io/912 v int c = 1427; float p = ; printf(“%#o, %#x, %#X, %g, %#g\n”, c, c, c, p, p); 02623, 0x593, 0X593, 1427, # Examples octalhex remove '.' and 0's do not remove '.' and 0's

Adv. UNIX:io/ Field Widths v Specify minimum number of characters printed: –if too few characters, right justify output –if too many, ignore field width

Adv. UNIX:io/914 Example char *s = “hello”; int x = 23; printf(“%10d%10s\n”, x, s); printf(“%-10d%-10s\n”, x, s); 23 hello 23 hello width - = left justify 10

Adv. UNIX:io/915 Field Width (*) in printf()  Calculate field width based on the next printf() argument: #define FIELD_WIDTH 10 printf(“%*d\n”, FIELD_WIDTH, val); uses FIELD_WIDTH

Adv. UNIX:io/ Precision (.) in printf() v SpecifierEffect %dMin. no. of digits (pad with leading zeros) %e, %fNo. of digits after the decimal point %gNo. of significant digits %sMin. no. of characters from a string

Adv. UNIX:io/917 Example int i = 873; float f = ; char s[] = “Happy Birthday”; printf(“%.4d\n%.9d\n”, i, i); printf(“%.3f\n%.3e\n%.3g\n”, f, f, f); printf(“%.11s\n”, s); e Happy Birth

Adv. UNIX:io/918 Meaning of *.*  Calculate field width and precision from the next arguments of the printf() printf(“%*.*s\n”, field_width, precision, str);

Adv. UNIX:io/ Type Modifiers v Place h or l before d, o, x, or u v Place L before e, f, or g  LetterEffect hSpecify short type lSpecify long type L Specify long double type

Adv. UNIX:io/ Formatted Input: scanf() v int i, x, y; float val; char str[100]; scanf(“%d”, &i); /* read in integer */ scanf(“%f”, &val); /* read in float */ scanf(“%s”, str); /* read in string */ scanf(“%d%d”, &x, &y); /* read space-separated integers */ hi -5 62

Adv. UNIX:io/921 Notes  %d will read a sign (e.g. -27 )  %f will accept a float in fractional or exponential form (e.g. 2.3, 1.2e+02 )  scanf() will ignore whitespace to get to a number –scanf("%d", &x); –whitespace includes new lines continued -27

Adv. UNIX:io/922  %s only reads non-whitespace characters –scanf("%s %s", str1, str2); –skips whitespace to start of text –stops reading in text when it gets to a whitespace –adds ‘\0’ –the string variable (e.g. str1 ) must have enough memory continued hello andy

Adv. UNIX:io/923 v scanf("%d%d", &x, &y); same as  scanf(“%d”, &x); scanf(“%d”, &y); scanf() will read x and y values from the same line, or any distance apart, so long as the values are separated by whitespace. continued 27 13

Adv. UNIX:io/924  %c will read any character (even whitespace): scanf(“%c”, &ch);  Other characters in the scanf() format string are used for input matching: scanf(“%d-%d %d”, &x, &y, &z); continued

Adv. UNIX:io/925  scanf() leaves non-matching characters on the input stream scanf("%d", &x); scanf("%s", str);  scanf() returns the number of bound variables, or EOF at end-of-file. int num; num = scanf("%d %d", &x, &y); hello continued

Adv. UNIX:io/926  scanf() uses line buffering –when scanf() is reading from the keyboard, it only gets a line to process when a RETURN is pressed

Adv. UNIX:io/ Assignment Supression (*) int month, day, year; printf(“Enter date as mm-dd-yy: “); scanf(“%d%*c%d%*c%d”, &month, &day, &year); printf(“month = %d day = %d year = %d\n”, month, day, year); Enter date as mm-dd-yy: 11-18_97 month = 11 day = 18 year = 97

Adv. UNIX:io/ Field Widths v A field width specifies the maximum number of input characters to process (ignoring whitespace before the start). v int x, y; scanf(“%2d%d”, &x, &y); printf(“Integers are: %d, %d\n”, x, y); Integers are: 12, 3456

Adv. UNIX:io/ Scan Sets [...] v Specify characters that can be read. v char z[9]; printf(“Enter String: “); scanf(“%[aeiou]”, z); printf(“The input was \”%s\”\n”, z); Enter String: ooeeooahah The input was “ooeeooa”

Adv. UNIX:io/930 Inverted Scan Sets [^...] v Specify characters not to be read. v char z[9]; printf(“Enter String: “); scanf(“%[^aeiou]”, z); printf(“The input was \”%s\”\n”, z); Enter String: Phreak The input was “Phr”

Adv. UNIX:io/ Handling Incorrect Input v Things to remember: –scanf() returns when it sees a character that it does not want. It leaves the character on the input stream; –scanf() returns the number of variables it has bound continued

Adv. UNIX:io/932 /* Try to read two integers */ #include int main() { int i, j, count; do { printf(“Enter two integers: “); count = scanf(“%d %d, &i, &j); if (count int main() { int i, j, count; do { printf(“Enter two integers: “); count = scanf(“%d %d, &i, &j); if (count < 2) {/* somthing wrong */ scanf(“%*[^\n]”);/* skip to end of line */ printf(“Incorrect. Try again: “); } } while (count < 2);/* until valid input */ : } * supresses assign; [^\n] matches upto newline (whitespace)

Adv. UNIX:io/ Line I/O  int puts(char *line); –e.g. puts(“Hello World”); –Adds a newline to its output. –If there is an error it returns EOF ; If okay it returns a non-negative integer.

Adv. UNIX:io/934  char *gets(char *line); –e.g: gets(str); –Reads to a newline in stdin ; replaces newline by ‘\0’ in input argument –gets() returns a NULL when EOF or an error occurs. –Main problem: gets() may read in a bigger string than can be stored in the variable; use fgets() instead. Bad style: avoid gets()

Adv. UNIX:io/935  char *fgets(char *line, int n, FILE *stream); –e.g: fgets(str, 80, stdin); –Reads at most n-1 chars into line, stopping if a newline is encountered; –the newline is included in line, and a ‘\0’ is added –fgets() returns a NULL when EOF or an error occurs.

Adv. UNIX:io/936 sscanf()  int sscanf(char *s, const char *format,...);  Same as scanf() but its input comes from the string argument ( s ) not from stdin. Very useful for separating reading from processing in code.

Adv. UNIX:io/937 Read then Process v #include int main() { char str[120]; /* magic number: poor style! */ int i, j; while (fgets(str,120,stdin) != NULL) { /* read */ /* process */ if (sscanf(str, “%d %d”, &i, &j) == 2) printf(“i = %d j = %d\n”, i, j); else if (sscanf(str, “%d”, &i) == 1) printf(“i = %d j = ??\n”, i); else printf(“Could not understand anything!\n”); : }

Adv. UNIX:io/ Character I/O v int putchar(int c); –e.g.: putchar(‘\n’); putchar(32); v int getchar(void); –e.g.: int ch; ch = getchar(); while ((ch = getchar()) != EOF)... Remember the brackets.

Adv. UNIX:io/939 Cannot “Press any key” #include int main() { char ch; printf(“Press any key to go on...”); ch = getchar(); /* type conversion */ printf(“You pressed ‘%c’\n”, ch); : } v Problem: UNIX line buffering  Quick fix: say “Press RETURN”