© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Input and Output.

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

1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
 2007 Pearson Education, Inc. All rights reserved C Formatted 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",
Programming in C #2 Input / Output.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Printing. printf: formatted printing So far we have just been copying stuff from standard-in, files, pipes, etc to the screen or another file. Say I have.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
 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.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
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.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Chapter 9 C Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
File Input/Output.
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.
Formatted Input/Output
Programming in C Input / Output.
Lecture 13 Input/Output Files.
Formatted Input/Output
Chapter 9 - Formatted Input/Output
Programming in C Input / Output.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Introduction to C EECS May 2019.
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output

© Janice Regan, CMPT 102, Sept Your first program /* My first C program */ /* make the computer print the string “Hello world” */ /* connect to any necessary libraries */ #include /* the main function, implements the algorithm to solve the problem*/ /* the main function may use functions from included libraries */ int main ( ) { printf("Hello, world!\n"); }

© Janice Regan, CMPT 102, Sept General form for printf  printf( “format string”, arg1, arg2, ….);  The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values)  The control string should have one conversion field for each argument to be printed.  If there are too many conversion fields they are printed with unpredictable results  If there are not enough conversion fields, the variables without conversion fields will be ignored

© Janice Regan, CMPT 102, Sept printf: Writing results  The format string contains text and conversions  Text includes explanations you wish to include in your output, punctuation, and spaces  Conversions are instructions giving the format to print each variable in the print statement.  The contents of your format string will be printed on the output line  Conversion fields will be replaced with the values of the indicated variables using the specified conversion. The conversion type is should be of the same type as the variable

© Janice Regan, CMPT 102, Sept Example of printf statement /* Print the sum. */ printf("The sum is %12.2f ", sum);  The %12.2f is the conversion, all other characters (including blanks) between the " " will be printed directly to the output line  The %12.2f will be replaced by the value of the variable sum when it prints to the output line  The variable sum will be printed with 2 digits after the decimal place, and using a total of at least 12 characters including the decimal place.  If less than 12 digits (9digits decimal 2 digits) are needed the extra digits will be replaced by blanks

© Janice Regan, CMPT 102, Sept Using the printf statement /* Print the sum. */ printf("The sum is %12.2f ", sum);  To print  (number preceded by 6 blank spaces)  To print  (number preceded by 3 blank spaces)  To print 4.58 *10 10  (no preceding blank spaces)  If it requires more digits to print the number than is provided by the conversion the number will be printed in the smallest number of digits possible consistent with the conversion, even if this number is larger than the value specified

© Janice Regan, CMPT 102, Sept Conversions for printf: 1  A % to indicate the start of the conversion field  An optional flag field which may contain  – left justify  +prepend + or –  space negative numbers printed with a preceding – positive numbers printed with a preceding blank )  An optional number indicating minimum field width to be printed (minimum # of characters reserved)  An optional period separating the field width from the precision ( not present for integer types)  A optional number indicating the precision or the number of digits displayed after the decimal in a floating point number

© Janice Regan, CMPT 102, Sept Conversions for printf: 2  An h or an l indicating short or long  A conversion character  d, i, u for integers and unsigned integers  f for doubles and floats,  c for characters  e for exponential format floating point number  g for general format, if exponent is <-4 or greater than the precision uses e otherwise uses f  Note: to print a % to your output you must put % in your format statement

© Janice Regan, CMPT 102, Sept Special characters in printf statements  There are other strings of characters, called escape sequences, you can place within your format statement to produce particular results  \n newline: move to the next output line  \b backspace: move back one space before printing the next character  \t horizontal tab  \v vertical tab  \\ print a \\? Print a ?  \" print a “\’ print a ‘  Note that a format statement should not contain the character produced by typing

© Janice Regan, CMPT 102, Sept Printing Integers: Examples: 1 int int1 = 2468, int2 = , int3 = -75; long int int4 = L; printf( "%d\n", int1); printf( "%d%d\n", int1, int2); printf( "%+d, %d, %-d, %-d\n", int1, int2, int3, int1); printf( "%8d, % d,\n %8d, %d\n", int1, int2, int3, int4); , , -75, , , -75,

© Janice Regan, CMPT 102, Sept Printing Integers: Examples: 2  Things to note from Printing Integers: Examples: 1  When there is no space between conversions, there are no spaces between the printed outputs. This is why second line of output looks like one number instead of two  Anything you wish to appear in the output that is not the value of a variable must appear in the format statement  The number of variables in the variable list must match the number of conversions in the format statement  When we specify a minimum field width (like the 8 in the last printf statement) remember that the sign counts as a digit.  When you want a new output line use a \n after the last thing (character or conversion) you want on the present line  If you put a space after \n you will get a space at the beginning of the next line  Remember the quotation marks around the format statement

© Janice Regan, CMPT 102, Sept Printing Integers: Examples: 3 unsigned int int2 = U, int1 = U; long int int3=-4567L, int4 = L; printf( "%i, %i\n", int1, int2); printf( "%u, %u\n", int1, int2); printf( "%12d, %7d, %10d\n", int3, int3, int4); printf( "%-12d, %+7d, %+10d\n", int3, int3, int4); printf( "% 12d, % 7d, % 10d\n", int3, int3, int4); , , , -4567, , -4567, , -4567,

© Janice Regan, CMPT 102, Sept Printing Integers: Examples: 4 More things to note Printing from Examples  %d and %i both print signed integers  %u should be used to print unsigned integers.  If %d or %i is used to print an unsigned integer then the correct integer may not be displayed  If the unsigned integer is small enough to be represented as a signed integer of the same length the displayed integer will be the same ( as for in the example)  If the unsigned integer is too large, for example the integer in the example, then the number printed will not be the value of the unsigned integer An integer that is ‘too large’ has a 1 in the most significant binary bit, the sign bit for a signed integer. The integer is displayed as a negative number (sign bit 1) rather than the larger unsigned integer.

© Janice Regan, CMPT 102, Sept Printing Integers: Examples: 5 More things to note Printing from Examples  The minimum width of a field can be specified  The 7 in %7d  A field 7 characters wide will be used to print the integer, the field will be padded (on the left) with blanks if necessary. The field may be wider than 7 characters if the number requires it  Numbers can be left justified.  The – in %-12d  This means any padding with blanks happens on the right rather than on the left. The number is printed with the first digit in the leftmost position  You can choose to display + on a positive number and – on a negative number (Use the + in %+7d or in %+12d )  You can leave a space for a sign, but display it only if it -. (Use the space as in % 7d )

© Janice Regan, CMPT 102, Sept Printing float and double variables float flt1 = 1.001, flt2 = , flt3 = -4.5E12; float flt4 = 66.54E-08; printf( "%f, %f, %f\n", flt1, flt2, flt3); printf( "%5.3f, %9.3f\n", flt1, flt2); printf( "%e, %+e, \n %8.4e, %+12.3e\n", flt3, flt4, flt3, flt4); printf( "%g, %g, %g, %g\n", flt1, flt2, flt3, flt4); , , , e+12, e-07, e+12, e , , -4.5e+12, 6.654e-07

© Janice Regan, CMPT 102, Sept Printing float and double variables 2  Float and double variables can be printed as decimal numbers mmmm.pppppp using the %f conversion  the precision or the number of digits after the decimal point can be specified. If it is not specified it will default to 6.  The.3 in %5.3f (3 digits follow the decimal point) is an example of specifying the precision  The width of the field includes the decimal point and sign  Float and double variables can also be printed in exponential form, mmmm.ppppppE±kk  The precision works the same as for the decimal format  The.4 in %8.4e (4 digits follow the decimal point) is an example of specifying the precision  The width of the field includes the decimal point, the E, the sign and the sign and digits of the exponent

© Janice Regan, CMPT 102, Sept Printing Long Double Numbers long double flt5 = L; printf( "%Lf\n ", flt5); printf( "%f\n ", flt5); printf( "%Le\n", flt5); printf( "%e\n", flt5); e e-18 l

© Janice Regan, CMPT 102, Sept Print functions  printf(“format string”, arg1, arg2, …);  Prints to the standard output  fprintf( file, “format string”, arg1, arg2, ….);  Prints to a file  sprintf( buff, “format string”, arg1, arg2, ….);  Prints to a buffer

© Janice Regan, CMPT 102, Sept General form for printf  printf( “format string”, arg1, arg2, ….);  The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values)  The control string should have one conversion field for each argument to be printed.  If there are too many conversion fields they are printed with unpredictable results  If there are not enough conversion fields, the variables without conversion fields will be ignored

© Janice Regan, CMPT 102, Sept Sending your output to a file  printf sends your output to the standard output device, usually the monitor screen  There are two approaches to sending output to a file  Redirection of the output  Writing output directly to the file using fprintf  The second approach gives more flexibility  You can write part of the output to a file and part to your screen (output to file, prompts to screen)  You can write different parts of your output to different files (Summary output to one file, detailed output to another)  fprintf requires that you identify the file the data is to be written into

© Janice Regan, CMPT 102, Sept Redirection of output  All information that would normally be written to the standard output (screen) will be written into file outfile instead ./myprog > outfile File outfile will be created if it does not exist, then the output will be written into file outfile The command will fail if outfile already exists ./myprog >> outfile File outfile will be created if it does not exist, then the output will be written into file outfile If file outfile already exists the output will be appended to the end of file outfile

© Janice Regan, CMPT 102, Sept General form for printf  fprintf( filep, “format string”, arg1, arg2, ….);  The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values)  The control string should have one conversion field for each argument to be printed.  The filep identifies the file into which the data is to be written  The file pointer, filep, has a special type. FILE  The file pointer provides a reference to the desired file  To associate the file pointer with the file, the file must be opened

© Janice Regan, CMPT 102, Sept  To open any file we need to know  If we will be reading from the file " r"  If we will be writing to the file (overwriting existing contents of the file) "w"  If we will be appending our output to the end of an existing file. "a“  The name (filepath) of the file we wish to ope  filep = fopen("filename", "w")  The value of the variable filep is of type FILE *, a reference to a file. The variable filep is used to reference the file once it has been opened. The fopen function

© Janice Regan, CMPT 102, Sept Using the fprintf statement: 1  When you declare variables you must also declare a variable of type FILE which will be used to reference the file within your program  FILE * DataFileExp1  Before you can read data from a file you must open it using fopen  DataFileExp1 = fopen("filename", "w");  This connects the variable DataFileExp1, known as a file pointer (it points to or references the file) to the file with name filename

© Janice Regan, CMPT 102, Sept Using the fprintf statement: 2  Once the file is opened you can read data  fprintf(DataFileExp1, “format string”, arg1, …);  This will write arguments arg1, … (with the format specified in the format string) to the file pointed to by DataFileExp1 (filename).  When you are finished writing to the file you must close the file filename  fclose(DataFileExp1);  This assures that the last of your data is written into the file (not left in a buffer)

© Janice Regan, CMPT 102, Sept Precision of representations in C short maximum: int maximum: long maximum: float precision digits: 6 float maximum exponent: 38 float maximum: e+38 double precision digits: 15 double maximum exponent: 308 double maximum: e+308 long precision digits: 18 long maximum exponent: 4932 long maximum: e+4932 For the gcc compiler in cygwin

© Janice Regan, CMPT 102, Sept General form for scanf  scanf( “format string”, &arg1, &arg2, ….);  The format string contains characters and conversion fields for the variables to be read  The format string should have one conversion field for each argument to be read.  & is the unary address operator, it tells the computer to print the value read at the address reserved for the variable

© Janice Regan, CMPT 102, Sept scanf: Reading results  The format string contains text and conversions  Text (not blanks or tabs or newlines) must match text in the input stream  Conversions are instructions giving the format to read each variable in the read statement.  The contents of your format string will be read from the standard input (usually the keyboard)  scanf continues reading until all conversions in the format string have been used or until an end of file is received, or until a conflict occurs ( the next character does not match the identifier or the expected text)

© Janice Regan, CMPT 102, Sept Conversions for scanf: 1  A % to indicate the start of the conversion field  An optional assignment suppression character  An optional number indicating maximum field width to be printed  An h or an l indicating short or long  A conversion character  d, i, u for integers and unsigned integers  e, f, g for doubles and float,  c for characters, s for a string of characters

© Janice Regan, CMPT 102, Sept scanf example  Consider the input data stream: First test, the 1st input is 3.65, the 2nd is 234, the last is 1.234e-05  This can be read using scanf in the following ways:  scanf( “ First a test, the 1st input is %4.2f, the 2nd is %3d”, the last is %9e”, &flvar1, &intvar2, &expvar3);  scanf( “ %*s %*s %*s %*s %*s %*s %4f %*s %*s %*s %3d %*s %*s %*s %9e”, &flvar1, &intvar2, &expvar3);  scanf(“%*31c %f %*11c %3d %12c %9e ”, &flvar1, &intvar2);

© Janice Regan, CMPT 102, Sept read functions  scanf( “format string”, arg1, arg2, ….);  Reads from the standard input  Usually the keyboard  fscanf( file, “format string”, arg1, arg2, ….);  Reads from a file  sscanf( buff, “format string”, arg1, arg2, ….);  Reads from a buffer

© Janice Regan, CMPT 102, Sept General form for fscanf  fscanf(filep, “format string”, &arg1, &arg2,...);  The filep ( type FILE) references or points to the file being read from  The format string contains characters and conversion fields for the variables to be read  The format string should have one conversion field for each argument to be read.  & is the unary address operator, it tells the computer to print the value read at the address reserved for the variable

© Janice Regan, CMPT 102, Sept Using the fscanf statement: 1  When you declare variables you must also declare a variable of type FILE which will be used to reference the file within your program  FILE * DataFileExp1  Before you can read data from a file you must open it using fopen  DataFileExp1 = fopen("filename", “r");  This connects the variable DataFileExp1, known as a file pointer (it points to or references the file) to the file with name filename

© Janice Regan, CMPT 102, Sept Using the fscanf statement: 2  Once the file is opened you can read data  fscanf(DataFileExp1, “format string”, arg1, …);  This will write arguments arg1, … (with the format specified in the format string) to the file pointed to by DataFileExp1 (filename).  When DataFileExp1 == NULL you have reached the end of the file. You can test for end of file using  Flag = feof( DataFileExp1); If Flag == 1 you are at the end of the file  When you are finished writing to the file you must close the file filename  fclose(DataFileExp1);