Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.

Slides:



Advertisements
Similar presentations
Chapter 8 Improving the User Interface
Advertisements

Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *
1 Basic I/O Input and Output. 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number.
Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 2 Basic Elements of Fortan
Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
27 April, 2000 CS1001 Lecture 25 Files Internal Files.
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.
CHAPTER 3 INPUT/OUTPUT List-directed (free formatted) input/output As in section 2.6 Formatted input/output General Form: Read format-specifier, input-list.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Chapter 8 Arrays and Strings
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Generalized I/O. Mystery of stars I/O statements took the form read *, x, y,z print *, a,b,c Such I/O is said to be List-directed or in free format user.
© 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.
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.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Input, Output, and Processing
Lecture 31 Numeric Edited Alphabetic (A) AlphaNumeric (X) Numeric (9, V, S) Numeric Edited (9, Z, comma, decimal point, minus sign) –Z = zero suppressed.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Why Files? ♦ the amount of data read and / or produced is huge ♦ repetitive data is needed for more than one program ♦ data may be entered by other people.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 05 (Part III) Control Statements: Part II.
INPUT / OUTPUT STATEMENTS
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
21 April, 2000 CS1001 Lecture 22 Formatted Input and 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.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5+ Input / Output Sec. 5.4 & 5.5 (p. 283 – 295) File Processing ( 檔案處理 )
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Week 9 Controling I/O. Today’s program Review of the last quiz Q&A about last weeks topics New topic: I/O formatting.
Topics Designing a Program Input, Processing, and Output
Chapter 9 C Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 part #3 C++ Input / Output
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
Formatted Input/Output
INPUT & OUTPUT scanf & printf.
Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
FILE PROCESSING Opening Files Why Files?
Formatted Input/Output
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Week 10 FILE PROCESSING.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

Chapter 5 Input / Output

2 Control over input & output  The input and output are basically facilitates a communication between the user and the program. Keyboard Disk file Magnetic type etc. Display Printer Disk file Magnetic type etc. Convert to internal form Convert to external form Real Integer Character etc. Input: Output: Computer’s memory External media Input and output editing

3 Types of I/O Statements There are two basic types of I/O statements in Fortran 90 1) List-directed (Without Formatted) 2) Formatted (Programmer Formatted)

4 Formatted Output  The simpler of two output statements in Fortran is print statement print format-specifier, output-list where format-specifier is one of the following: 1) * (an asterisk) 2) A character constant or a character variable (or expression or array) whose value specifies the format for the output. 3) The label of a format statement print statement displays the values of the items in the output- list on a new line.

5 Formatted Output  The format-specifier specifies the format in which values of the expressions in the output list are to be displayed.  In the second type of format-specifier, the formatting information is given as a character string that consists of format descriptors, separated by commas and enclosed in parentheses: ‘(list of format descriptors) ‘ or “(list of format descriptors) “  In the third case, the formatting information is supplied by a format statement whose label is specified. This statement has the form label format (list of format descriptors) where label is an integer in the range 1 through

6 Formatted Output  For some Fortran compilers, the first character of each line of output directed to a printer is used to control the vertical spacing. black Normal spacing: advance to the next line before printing 0 Double spacing: skip one line before printing 1 Advance to top of next page before printing + Overprint the last line printed There are many format descriptors that may be used in format specifiers. A list is presented in the following table.

7 Format Descriptors

8

9 Formatted Output Example: print ‘ (I3) ‘, N The format descriptor I3 specifies that the value to be printed is an integer and is to be printed in the first three positions of a line. If the value of N is 15, the three positions are filled with _15 (where _ denotes a blank).

10 Integer Output- The I descriptor  Integer values that are output using an I descriptor are right- justified in fields of the specified sizes; that is each value is displayed so that its last digit appears in the rightmost position of the field. For example, if the values of the integer variables Number, L, and Kappa are Integer :: Number = 3, L = 5378, Kappa = then the statements print `(1X, 2I5, I7, I10)`, Number, Number -3, L, Kappa print `(1X, 2I5.5, I7, I10.7)`, Number, Number -3, L, Kappa print `(1X, 2I5.0, I7, I10)`, Number, Number -3, L, Kappa or

11 Integer Output- The I descriptor Print 30, Number, Number -3, L, Kappa Print 31, Number, Number -3, L, Kappa Print 32, Number, Number -3, L, Kappa 30 format (1X, 2I5, I7, I10) 31 format (1X, 2I5.2, I7, I10.7) 32 format (1X, 2I5.0, I7, I10) produce the following output: |_ _ _ _ _ 3 _ _ _ _ 0 _ _ _ 5378 _ _ _ _ |_ _ _ _ 0 3 _ _ _ 0 0 _ _ _ 5378 _ _ |_ _ _ _ _ 3 _ _ _ _ _ _ _ _ 5378 _ _ _ _

12 Real Output- The F, E, ES and EN Descriptors  Integer and real outputs are right-justified in their fields. In a Fw.d descriptor, w specifies the total width of the field in which to display a real value, and d is the number of digits to the right of the decimal point. Example: integer :: In = 625, Out = -19 real :: A = 7.5, B = 0.182, C = using the statement print `(1X, 2I4, 2F6.3, F8,3)`, In, Out, A, B, C The resulting output is |_ _ _ _ _ _

13 Real Output- The F, E, ES and EN Descriptors  For a descriptor Fw.d, one should have w >= d+3 to allow for the sign of the number, the first digit, and the decimal point (-,0.). Let’s see the E descriptor with an example: The values of real variables A, B, C and D are given by real :: A= E8, B=0.0237, C=4.6E-12, D= E12 the statement print ‘(1X, 2E15.5, E15.4, E14.4)’, A, B, C, D produces output like the following _ _ _ _ _ E+08_ _ _ _ E-01_ _ _ _ _0.4600E-11_ _ _ E+14 As with the F descriptor, a field is asterisk-filled if it is not large enough for the value.

14 Real Output- The F, E, ES and EN Descriptors  The scientific descriptor ES is used in the same manner as the E descriptor. Only difference is that the values are normalized.  The engineering descriptor EN also is used in the same manner as the E descriptor. Only difference is that exponent is constrained to be a multiple of 3.

15 Character Output  Character data may also be displayed by using an A format descriptor of the form rA or rAw. In the first form, the field width is determined by the length of the character value being displayed.  In the second form, if the field width exceeds the length of the character value, that value is right-justified in the field.

16 Positional descriptors - X and T  A descriptor of the form nX can be used to insert n blanks in an output line. A tab descriptor of the form Tc causes the next output field to begin at the specified position c on the current line. For example: print 75, “John Q. Doe”, “CPSC”, Number 75 format (1X, A11, 3X, A4, 2X, I3) or 75 format (1X, A11, T16, A4, 2X, I3) will produce the output _ John_Q._Doe_ _ _CPSC_ _141

17 Repeating Groups and The Slash (/) Descriptor  It is possible to repeat some format descriptors by preceding them with a repetition indicator. For example  3F10.2 is equivalent to F10.2, F10.2, F10.2.  A single output statement can be used to display values on more than one line, with different formats, by using a slash (/) descriptor. Consider the following statement: print (fmt=88) “Values”, N, A, M, B, C, D 88 format (1X, A, 3/ 1X, 2(I10, F10.2) // 1X, 2E15.7)

18 Formatted Input read statement has two forms: the simpler form is, read format-specifier, input-list For example: read 5, I, J, K INTEGER INPUT Integer data can be read using the I descriptor of the form rIw, where w indicates the width of the field. For example: integer :: I, J, K read ‘(I6, I4, I7)’, I, J, K or read 5, I, J, K 5 format (I6, I4, I7)

19 Formatted Input REAL INPUT We can enter a real number in 2 ways: 1) Enter the numbers without decimal points. For example: enter the following values for real variables A, B, C, D and E. A=6.25, B=-1.9, C=75.0, D=.182, E= read ‘(F3.2, 2F3.1, F3.3, F6.3)’, A, B, C, D, E enter the data in the form ) Enter the decimal point as part of the input value read ‘(4F5.0, F8.0)’, A, B, C, D, E enter the data in the following form _6.25_-1.9_ _75._.182_

20 Formatted Input CHARACTER INPUT  When a read statement whose input list contains a character variable is executed, all characters in the field associated with the corresponding A descriptor are read. For example: Assume the line of data is: Fourscore_and_seven_years_ago and read by statements character(6) :: Speech1,Speech2 read ‘ (2A) ‘, Speech1, Speech2 the values assigned to Speech1 and Speech2 are: Speech1: Foursc Speech2: ore_an

21 Reading & Exercises ELLIS BOOK  Do example 3.2 and self-test exercise 3.2 on pages 60 and 63.  Read pages between 56 and 60.

22 Write Statement The write statement has the following form write (control-list) output-list where control-list may include items selected from the following: 1) A unit specifier, which is a integer expression whose value designates the output device unit = unit-specifier or simply unit-specifier 2) A format specifier that may be any of the forms allowed in the print statement. fmt = format-specifier or simply format-specifier 3) An ADVANCE=clause of the form advance =character-expression where the value of character-expression is either “no” or “yes”. 4) Any other items that are especially useful in file processing.

23 Read Statement The read statement has the following form read (control-list) input-list where control-list may include items selected from the following: 1) A unit specifier, which is a integer expression whose value designates the input device unit = unit-specifier or simply unit-specifier 2) A format specifier that may be any of the forms allowed in the print statement. fmt = format-specifier or simply format-specifier 3) An ADVANCE=clause of the form advance =character-expression where the value of character-expression is either “no” or “yes”. 4) An iostat=clause to detect an input error or end-of-file condition.

24 Reading & Exercise ELLIS BOOK  Read pages between 230 and 258.  Study self-test exercises 9.2 and summary.

25 File Processing  Storing/reading large data sets in/from a file are more convenient for data processing. OPENING FILES In order to open a file in Fortran a unit number must be connected to it and several items of information about the file must be supplied. Open statement is the following form: open (open_specifier_list) where open-list includes: 1) unit = indicating a unit number. 2) file = (character-expression ) indicating the name of the file. 3) status = (character-expression ) where the value of character- expression is one of

26 File Processing (Opening files) “old” “new” “replace” (or unknown). And “scratch” 4) action= allowed_actions where allowed_actions are a character expression whose value is one of “read” “write” “readwrite” (default) (status=scratch) 5) position=character-expression where the value of character- expression is one of “rewind” (The file at its initial point) “append” (The file at its end)

27 File Processing (Opening files) “asis” (Leave its position unchanged) 6) IOSTAT=status-variable where status-variable is an integer variable to which the value zero is assigned if the file is opened successfully and a positive value is assigned otherwise. NOTE: If file name is not presented then status=“scratch” must be specified EXAMPLE: open (unit = 12, File = “INFO.DAT”, status = “old”, action = “read”, position = “rewind”, iostat = OpenStatus) where OpenStatus is an integer variable. Sometimes it is convenient to declare a character variable to store the name of file in a way that the user input the name.

28 File Processing (Opening files) EXAMPLE: Character (12) :: FileName write (unit=*, ‘(1X, A)’, advance =‘no’) “Enter the name of data file:” read (unit=*, fmt=*) FileName and then use the following open statement: open (unit = 12, file = FileName, status =“old”, action = “read”, & position = “rewind”, iostat = OpenStatus) if the file is successfully opened, the status variable OpenStatus will be set to 0. If (OpenStatus > 0) stop “ *** cannot open file *** “

29 File Processing (Closing files) The close statement has a function opposite that of the open statement and it is used to disconnect a file from its unit number. Close (close-specifier-list) where close-specifier-list must include a unit specifier and may include other items such as iostat and status. status may take character expressions such as keep or delete. FILE INPUT/OUTPUT Once a file has been disconnected to a unit number, data can be read from or written to that file using the general forms of the read and write statements. During this process iostat can be positive negative or zero. 1) A positive value; in case of an input error occurs 2) A negative value; if the end of data is encountered but no input error occurs

30 File Processing (File Input/Output) 3) Zero value; if neither an input error nor the end of data occurs. Example: do read (12, fmt=*, iostat = InputStatus) Code, Temperature, Pressure ! If end of data, terminate repetition if (InputStatus < 0) exit ! If input error, stop execution if (InputStatus >0) stop “ *** Input error ***” ! Otherwise continue processing data Count = Count + 1. SumOfPressures+Pressure end do

31 File Processing ( rewind/backspace )  Sometimes it is necessary to reposition a file so that data values that have already been read can be read again. To reposition a file at its beginning use a rewind statement rewind (auxlist) To reposition a file at the beginning of the proceeding line use backspace statement backspace (auxlist) auxlist consist of a unit specifier and, optionally, an iostat specifier.

32 End of File  To permit detection of the end-of-file mark, an exeption specifier of the form iostat=Integer variable may be included in the Control list of the read statement: read (unit = *, fmt = *, iostat = Integer_variable) Input list The designated Integer variable will be zero after a normal read operation; it will have a negative value if an end-of-file mark is read. Example: integer :: E_O_F do read (unit=*, fmt=*, iostat=E_O_F) Next_Datum ! Set E_O_F if (E_O_F < 0) exit ! Test E_O_F. enddo

33 Exercises on I/O operations ELLIS BOOK  Read pages between  Do self exercises 9.1 and 9.2 on pages 248 and 254. Exercises on File processing  Read pages between