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.

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 *
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
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:
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
27 April, 2000 CS1001 Lecture 25 Files Internal Files.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
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.
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.
Chapter 3: Input/Output
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Example in Final Fall 2006 PROGRAM EXAM IMPLICIT NONE INTEGER :: A=3, B=8 REAL :: CALC B = CALC(A, B) A = CALC(A, B) WRITE(*,*) A, B END PROGRAM EXAM a)
Topics Covered: Data preparation Data preparation Data capturing Data capturing Data verification and validation Data verification and validation Data.
© 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.
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.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Input, Output, and Processing
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Printing on power systems Program/ Command Data Report Layout (Printer File) Job Output Queue *FILE Spooled File.
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.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
FILE HANDLING IN C++.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
INPUT / OUTPUT STATEMENTS
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Ch 91 Pipes, Filters and Redirection. Ch 92 Overview Will use redirection to redirect standard input and standard output.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
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.
Learners Support Publications Working with Files.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Files A collection of related data treated as a unit. Two types Text
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
SVBIT SUBJECT:- Operating System TOPICS:- File Management
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
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.
Programming in C Input / Output.
Topics Introduction to File Input and Output
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Handling.
Chapter 3: Input/Output
Topics Introduction to File Input and Output
FILE PROCESSING Opening Files Why Files?
Topics Introduction to File Input and Output
Week 10 FILE PROCESSING.
Presentation transcript:

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 has no control over the appearance of I/O No flexibility over the I/O devices Keyboard input Display unit output * indicates no control or default format

Mystery resolved * stands for (*,*) In general, it will be (arg1,arg2) arg1 used to select the I/O device –* indicates default devices (keyboard and monitor) arg2 controls the I/O formatting –It is a string constant –* means default formatting

Files I/O through files is the most convenient Standard I/O is very painful for large data Large amount of data can be stored and retrieved with ease Output of one program can be used as input to another The programs need not be running at the same time Once created they remain - permanent memory Files are high level software abstractions of these permanent memory devices They are called as secondary memory

Secondary Memory permanent, slow and large volume of data Some kind of magnetic memory – no power required to retain unlike main memory different kinds - Hard Disks, Floppy Disc, CDs, Magnetic Tapes (old) Random or Sequential access details of devices abstracted from application programmers Files are the abstractions Std. I/O are also files

File Organization Fortran programmer's view A simple file is a linear sequence of records Each record is a meaningful piece of data Files are read or written one record at a time Each record is typically a line of data –creation of file determines type of data Files can be sequential or random access Random access files require keys

File Operations Opening a file writing or reading a record Rewinding a file Closing a file Valid sequence – (open (read/write/rewind)* close)* File operations translated to OS calls They can fail and graceful recovery possible

File Opening Simplest example is: open(unit = 23, file = "input.dat") This commands associates the file input.dat with the number 23 This number can be used for reading and writing onto the file Example:read(23, *) N write(23, *) M Unit numbers are system dependent (usually from 1 to 100) unit and file are named parameters Contrast this with positional parameters

Open attributes Many file attributes assume default values in the above to override these, additional parameters to be set action =READ, WRITE or READWRITE (default) status =OLD, NEW, REPLACE (default), SCRATCH, UNKNOWN ios = x, x is assigned a value at the end of the call 0 - if the operation is successful > 0 - otherwise access = "sequential" (default) or "direct"

Some more attributes form = "formatted" (default) or "unformatted“ –in unformatted form, data stored in some internal representation –can not be edited recl = length of each record –required for direct access files position = "rewind", "append"

Example integer:: ierr open(unit=23, file = "in.dat", status = "old", action = "Read“, iostat = ierr) –opening the existing file "in.dat", for read –ierr gets a value > 0 iff failure in opening

Another Example integer:: ierr integer:: ut = 34 open(unit= ut, file = "out.dat", status = "new", action = "write“, iostat = ierr) – creating a new file "out.dat – Status = replace would replace the file

One more Example open(unit= 15, status = "scratch", iostat = ierr) –no file name specified - error otherwise –default action - reading and writing temporary data –erased after the program terminates

Closing a file Close(close_list) Close_list –must specify the unit number –may specify other clauses the number is released and can be reused in other open statements repeated opening and closing of non scratch files possible scratch file contents lost once they are closed

Reading and Writing files contain sequence of records Each record is read/written using a single read or write operation So we can say that each record occupies a single line Every read statement refers to the successive records Reference to the file through the unit number example: read(unit = 16, *) x, y, z write(unit = 25, *) "test output=", a, b, c read and write one record to the appropriate file

Read Pointer There can be errors while reading a file There is a read pointer that indicates the next record to be read Every read moves the read pointer to the next record (in sequential access) Read Pointer

Read Error The read pointer may be at the end use istat clause for detecting end of file (EOF) Read(16,*, iostat = err) x, y, z If err is non zero then there is an error Use always iostat clause otherwise, program will abort

Other parameters For direct access files, use the argument rec = n This reads/writes the nth record Example: write(23,*,iostat = err, rec = 15) i,j –write values of i, j to the record 15 of the associated file

Program bubble_sort implicit none integer:: N, i, rt_end, index, temp ! N - the total number of items in the array ! i, j - array index variables integer, dimension(100):: A integer:: err,errf, werr open(unit=1, file = "in.dat", & status = "old", iostat = err) if (err/=0) then print *, "file opening error" else outer: do

Program Continued read (unit =1, iostat = errf, fmt = * ) N read (1, *, iostat = errf) (A(i),i=1,N) if (errf /= 0) exit rt_end = N inner: do if (rt_end A(i+1)) then temp = A(i) A(i) = A(i+1) A(i+1) = temp index = i endif enddo rt_end = index end do inner

Program Continued open(unit=2, file = "out.dat", & status = "old", access = "sequential", & iostat = werr) print *, (A(i),i=1,N) write (2, fmt = *) (A(i),i=1,N) end do outer endif close(unit=1) end program

I/O Formatting So far we have used List-Directed I/O Statements (Free format) print *, A,B,C read *, A,B Fine for small programs and small number of inputs and outputs Many data processing applications manipulate large collection of inputs and outputs –Tabulating marks and results –preparing mark sheets and reservation charts Pretty printing and Formatted input very essential Fortran has elaborate features for formatting I/O

Format Strings I/O formats can be specified by using format strings print fs, A, B,C read fs, C,D fs identifies a format string –a constant string –a string variable –a label that labels a format statement containing the format string Here is an example format strings print "I4, I4, I4", A, B, C This prints A, B and C in a row with each being given 4 decimal places

Edit Descriptor I4 is an edit descriptor Format string contains one or more edit descriptors Very many edit descriptors that help print different types of variables Alternate format strings Character(len=20):: str str = "(I4, I4, I4, I4)” write(*, str) a,b,c,d read *, 100, a, b, c, d 100 format ("I4,I4,I4,I4") print "4I4", a, b,c,d

I/O buffer I/O done through buffers Buffer is a temporary storage space where every line of characters is composed before doing I/O Line-oriented I/O For reading – input process builds the buffer and gives the buffer content to the program Every print statement composes the output in the output buffer completely before sending for printing on the device Buffers are read/written as per the format statement

Output Buffer Usually 133 characters long (line-printer output) First character is a control character –1 skip to next page –Blank single spacing (default when * is used) –0 double spacing –+ no space – overwriting The rest of the characters are printed Make sure that first character is the intended control character print (*,100) n 100 format (I3) Behaviour of print statement when n = 25 and n =125

Some descriptors Iw - w columns reserved for printing an integer value includes - if the number is negative 5Iw - 5 successive w columns reserved for printing/reading 5 values Iw.m - minimum m digits need to be presented If w not enough then * printed If the number is less than m digits - leading zeros added

Format Descriptors rFw.d r - repeat count w - number of columns for the entire number (including decimal point and -ve sign) d - number of columns to the right of the decimal point Number appears right-justified Trailing zeros if the no. of digits after decimal point is less than d rounded off if d is more if (w-d-1) is less, then asterisks printed

E-descriptor rEw.d Printing is done in E notation Print "E11.4", A will print E-02 when the value of A is * 10 ** -1 Make sure that w > = d + 7

A-descriptor rA or rAw if w is more then A is right justifies if w is less than only first w characters of A are printed X descriptor nX inserts n spaces in the output T descriptor Tc moves to the column number c rLw used for printing logical variables - w columns are used output will be either T or F, right justified

Repetition Repetition can be applied to groups of descriptors 2(I6, 2X, 2F10.2) is used to print/read two groups, each consisting of an integer (6 digits) and two floating point numbers of size 10 / operator used for breaking the line A single write statement can print on more than one line

Descriptor Association print fs, var_list association of format descriptor to a variable is done from left to right if for every descriptor (except print controls) there is a variable and vice versa, then association is simple if less variables then rest of the descriptors discarded if more variables then the printing starts from a fresh line for rest of the variables format strings for these variables start from right most open parenthesis that does not have repetition count

Example print (8, 60) j,k,l,m,n 60 format (1X,"The value” =, /, (1X, ‘New Line', 2(3X, I5))) The output is (column number not printed) The value = New Line 1 2 New Line 3 4 New Line 5 where j - n has the value 1 - 5

Other aspects Formatted reading is similar (see the books/manuals for details) Format strings can be specified in generalized file-based I/O Include the format string in the argument fmt