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.

Slides:



Advertisements
Similar presentations
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=
Advertisements

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 *
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
CPS120: Introduction to Computer Science INPUT/OUTPUT.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
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.
Programming Logic and Design Fourth Edition, Introductory
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.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the.
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.
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
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.
 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.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
INPUT / OUTPUT STATEMENTS
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.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Chapter 5+ Input / Output Sec. 5.4 & 5.5 (p. 283 – 295) File Processing ( 檔案處理 )
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Week 9 Controling I/O. Today’s program Review of the last quiz Q&A about last weeks topics New topic: I/O formatting.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
C Formatted Input/Output
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Chapter 2 part #3 C++ Input / Output
Formatted Input/Output
Formatted Input/Output
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 3: Input/Output
Formatted Input/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.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

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 –controlled by the programmer –you can specify every detail of what is printed

Output statements n PRINT –only used for output to the screen n WRITE –used for output to files

PRINT statements n List directed –PRINT*, num n Formatted –PRINT ‘(1x, i5, f8.2)’, number, temperature n With format labels –PRINT 10, number, temperature –10 FORMAT (1x, i5, f8.2)

Format descriptors n (1x, i5, f8.2) –1 space, followed by a 5-digit integer, followed by a real number taking up 8 spaces total with two after the decimal point. n A format descriptor designated the exact number of characters that will be printed and where they will be printed.

The integer format descriptor n i used for integers, can be repeated (5i) and can have width specified (i5) –INTEGER :: num –num = 123 –PRINT ‘(i3)’, num > 123 –PRINT ‘(3i)’, num > –PRINT *, num > 123 –PRINT ‘(i2)’, num > **

The real number descriptor n F for ‘floating point’ (real) numbers. –Followed by an integer total field width, ‘.’, then an integer number of decimal digits. –REAL :: num –num = –PRINT ‘(f8.2)’, num > –PRINT ‘(f6.2)’, num > –PRINT*, num > –PRINT ‘(f3.2)’, num > ***

The character data descriptor n a used for ‘alpha-numeric data’ –CHARACTER(10) :: name –name = “Washington” –PRINT ‘(a10)’, name > Washington –PRINT ‘(a15)’, name > Washington_____ –PRINT*, name > Washington –PRINT ‘(a5)’ > Washi

The spacing descriptors n X - stands for ‘print a blank space’ n T - stands for ‘print a tab’ –INTEGER :: num1, num2 –num1 = 123 –num2 = 456 –PRINT ‘(1x,i5,5x,i3)’, num1, num2 – ___123_____456 –PRINT ‘(t5, i3), num > _____123

Difference between x and t n 5x gives five spaces n t5 tabs out to column 5 (gives only 4 spaces!) n When used in tables together they are easy to confuse.

Repetition and width nX if first, specifies line spacing nX n blanks or spaces printed A number of characters matches list item Aw w characters are printed Iw integer is right-justified in w columns Fw.d real no. is in w columns, d is decimal places

Carriage controls n FIRST EDIT DESCRIPTOR –‘ ‘ OR 1X PRINTS A SPACE –‘0’DOUBLE LINE SPACING –‘1’PRINTS ON TOP OF PAGE –‘+’PRINTS WITHOUT SPACING (OVERPRINTS) SOME COMPILERS DON’T RECOGNIZE ALL OF THE ABOVE

Repeating Edit Descriptors n Place a number in front of descriptor nI4repeats I4 n times 3F5.2repeats F5.2 3 times 4(5X,I2)repeats 5X & I2 4 times

More descriptors Twtabs over to column w /starts rest of statement on a new line n(/)starts rest of statement in n lines Ew.dreads numbers in scientific notation

Literals n Individual characters may also be inserted in formats. –PRINT ‘(“$”, f6.2)’, price > $ –PRINT ‘(10(“-”))’ > ==========

Printing tables n First print the heading n Then a loop fills in the body of the table –PRINT ‘(t10, “Number”, t20, “Square”)’ –DO n=1, 5 – PRINT ‘(9x, i6, t20, i6)’, n, n**2 –ENDDO – Number----Square – – – – –

READ statements n List directed –READ*, num n Formatted –READ ‘(1x, i5, f8.2)’, number, temperature n With format labels –READ 10, number, temperature –10 FORMAT (1x, i5, f8.2)

Reading real numbers n Data may come in in one of two ways –Numbers without decimal points the decimal digits are always filled –Numbers with decimal points –REAL :: num –READ ‘(f6.2)’, num –The data entered may be either with decimal point (123.45) or not ( 12345)

The WRITE statement n is used to write to files n Underlies the PRINT statement n PRINT*, is really WRITE (*,*) or WRITE (6,*) n By default, write will send output to the default output device. –This device is the monitor –It is identified by a number (UNIT = 6) –Often called ‘standard output’

WRITE default examples n WRITE (*, *) num1, num2 n WRITE (*, ‘(i4,1x,i6)’) num1, num2 n WRITE (*, 20) num1, num2 n 20 FORMAT (1x. I4, t12, i5) n WRITE (UNIT = 6, FMT = 30) num1, num2

READ default settings n is used to read from files n By default, read will read from the default input device –This device is the keyboard –It is identified by the number (UNIT = 5) –Often called ‘standard input’

Default devices n UNIT 6 is standard output n UNIT 5 is standard input n It is an error to try to read from standard output or write to standard input.

File processing n Data commonly comes to us in files. n We will learn how to handle –text files (ASCII characters) –rectangular, “flat files” –sequential access n Later the book will discuss –binary files –direct (random) access

Opening a file n To open a file use the OPEN statement. –First specify the UNIT number of the file –Then the name of the FILE –Other specifiers are STATUS (NEW, OLD, REPLACE) ACTION (READ, WRITE, READWRITE) POSITION (REWIND, APPEND, ASIS) IOSTAT (0 = ok, >0 = error)

Examples n OPEN (UNIT=12, FILE=“mydata.dat”, STATUS = “OLD”, ACTION=“READ”, POSITION=“REWIND”, IOSTAT=OpenStatus) n OPEN(12,FILE=“mydat”) n Both forms yield the same results due to default settings

Using IOSTAT n IOSTAT means Input/Output Status n If the value is placed in a variable it may be used later in the program. n OPEN (12,FILE=“mydata.dat”, IOSTAT=OpenStatus) n IF (OpenStatus > 0) STOP “**** Cannot open file ****”

CLOSE n Any file that is OPENed should also be closed after your program is done using it. n CLOSE (12) n Only the UNIT number needs to be specified.

READ, IOSTAT and END= n IOSTAT is used with READ to help a program stop reading from a file when it encounters the end of that file (eof). n READ (12, *, IOSTAT = InputStatus) num1, num2 n IF (InputStatus < 0) EXIT n Another method is READ (12,*, END=20) num1, num2 n 20 PRINT*, “The end of the file has been encountered”