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.

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

Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
Introduction to C Programming
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
27 April, 2000 CS1001 Lecture 25 Files Internal Files.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Introduction to Python
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.
Introduction to C Programming
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
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.
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
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.
 Pearson Education, Inc. All rights reserved Formatted Output.
Machine Instruction Characteristics
BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Indexed and Relative File Processing
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Chapter 9 I/O Streams and Data Files
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
INPUT / OUTPUT STATEMENTS
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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.
Chapter 2: Introduction to C++
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Chapter 2 part #3 C++ Input / Output
Programming in 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.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
INPUT & OUTPUT scanf & printf.
Chapter 3 The DATA DIVISION.
Iteration: Beyond the Basic PERFORM
CHAPTER 17 The Report Writer Module
Chapter 2 part #3 C++ Input / Output
Topics Introduction to File Input and Output
Week 10 FILE PROCESSING.
Presentation transcript:

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.  In order to work with files we need to focus on I/O operations, data transfer and file operations.

3 Information for data transfer  Data transfer characteristics are specified by the following items called control information. (1) The direction of data transfer (input or output) (2) The external device, or unit, to or from which data is to be transferred (3) The format description that specifies conversion between the computer-oriented internal form of data and its representation as a character string. (4) (Optional) Positioning action that is to occur at the end of data transfer (Advancing; if the file on the external device is to be repositioned to the end of the current record. Non advancing; if the file position is to remain in its place within the current record) (5) (Optional) Exception handling to be performed in the event of an end-of-file, end-of-record, or error condition during data transfer.

4 I / O Statements Direction of Transfer The direction of transfer is determined by the initial keyword in the statement. The keyword read denotes input, while write denotes output. External Device Each external device is identified with a unique integer value called a unit number. A given unit number represents the same device in the main program and in all of its modules and subprograms. Thus, unit numbers may be said to have global scope.

5 I / O Statements I / O Statement Forms An input or output statement has the following form. read (unit=2, fmt=”(2i5, a, 2f16.8)”) I, Code, Str, Bugle, Dpr write (unit=*, fmt=”(f10.3, i6, f10.3,a)”, advance =”no”) A, B, C,” # ” write (unit=*, fmt=*) Code, X+Y+Z The positioning specifier advance=”no” in the write statement states that the output file is not to be advanced to a new record after A, B and C have been written to the file. Thus the second write statement will append additional data to the same output record.

6 Data Transfer RECORDS Information on a file subdivided into records. A record corresponds roughly to a line on a terminal screen or to a printed line. The structure of unformatted records is processor-dependent, since the amount of space required on the external unit depends on the details of processor data representation. FORMATTED RECORDS A formatted record is composed of characters. These characters may include letters, digits, and special symbols from the ASCII character set. NONADVANCING INPUT AND OUTPUT Input and output always positions the input or output file, as a default option, at the end of the last record to or from which information has been transferred. In some cases, this style of input and output is not convenient. It would often be preferable to stop data transfer in the middle of an input or output record and to continue later with the remainder of the same record. Instead of default advancing file-positioning style, output statement may request a non advancing style, whereby the file position may remain within a record after the input or output statement has been executed. (See example 9.5, p400, Meissner`s book)

7 Format specifier LIST-DIRECTED FORMATTING An asterisk format specifier indicates list-directed formatting, which is adequate for input in most situations and for output in applications where the precise appearance of the results is not important

8 Explicit format control The following specification describes the arrangement of five fields within a record (3 f12.0, 2 i3) An input field can be specified by the following format code read (unit=*, fmt=“(3 e12.0, 2 i3)”) A, B, C, I, J Each format code in a format description includes the following information: 1) a count, indicating the number of consecutive fields to which it corresponds 2) a conversion mode, indicating by letters such as f, es, i, l or a. 3) a field width 4) a decimal position (optional) I Format code for integer input read (unit = *, fmt = “(i3, i4, 2 i6, i8, i3)” ) I, J, K, L, M, N external appearance field width format code internal value i i i i i _ 3 i3 0

9 Explicit format control f Format code for real output write (unit = *, fmt = “(t2, f10.3, f6.0, f8.2, f5.2, f6.3)”, A, B, B, C, H real variable internal value format code external appearance A f10.3 _ _ B 234. f6.0 _ _ 234. B 234. f8.2 _ _ C f H 0. f6.3 _ 0.000

10 Explicit format control f Format code for real input read (unit = *, fmt = “(f10.4, f8.0, f5.3, f13.6, f9.0, f8.4, f8.2)”, A, B, C, D, E, F, G real variable internal value field width external appearance A _ B _ _ + _ 234. C _ D E+02 E E-3 F e-3 G E3_ IMPORTANT: If a decimal point appears in the input field, the value of d in the format code is ignored; the actual decimal point overrides the format specification.

11 Explicit format control FORMAT RESCAN If there are more list items than format codes in the format description, the input or output process must continue to another record in order to complete the list. For example; suppose that we use the format description (f12.4, i3) which specifies an input record containing only two fields. If five data items are be read, the format must be rescanned to match the remaining items of the list. In the first of the following examples, the rescan point is near the middle of the format; in the second, the rescan point is at the repeat count that immediately follows the opening right parenthesis of the format. (2 (i5, i4, i3), f5.2, 2(i5, i4, i3), f5.2, i6) ^ rescan point (2 ( 2 (i5, i4, i3), f5.2), i6) ^ rescan point

12 Exercises  Study Format codes for Input and Output in Appendix B (Meissner’s book).

13 Format Specification  Format specification with named constant Instead of a literal character constant, a format specifier may be the name of a character string that contains the format description. program test implicit none real :: A, B, C integer :: J, K character (len = *), parameter :: F14 = “(3 f12.0, 2 i3)” read (unit = *, fmt = F14) A, B, C, J, K write (unit = *, fmt = F14) A, B, C, J, K stop end program test

14 Files  A file is an external source from which data may be obtained or an external destination to which it may be sent. Data can not be transferred to or from a file until the file is connected to the program. CONNECTION PROPERTIES A connection between a unit and a file has certain connection properties. When a unit becomes connected to a file by execution of an open statement, the following properties may be established: 1) An access method, sequential or direct, is always established. 2) A form, formatted or unformatted, is always established. 3) An initial position, rewind or append, specifies initial positioning of the file during execution of open statement 4) A permanence property, temporary or permanent, specifies disposition of the file when the connection is terminated. 5) An action property, read, write or read-write, specifies which kinds of

15 Files Data transfer statements are permitted. 6) A record length may be established. The OPEN Statement The statement consists of the keyword open followed by a control list. A control list must include the following specifiers. 1) status = Character expression, where the value of Character expression is old, new, replace, or scratch. 2) action = Character expression, where the value of Character expression is read, write, or readwrite. 3) position = Character expression, where the value of character expression is rewind or append. The followings are optional specifiers 4) access = Character expression, where the value of character expression is sequential or direct (default is sequential).

16 Files 5) form = Character expression, where the value of Character expression is formatted or unformatted (default formatted). 6) recl = Integer expression, where the value of Integer expression is positive. 7) iostat = Integer variable name. Here some examples for open statement: open (unit = 6, file =“My_Data.bin”, status =“old”, action =“read”,& form = “unformatted”) open (unit = J, file =“My_Input.txt”, status =“old”, action =“read”) open (unit = 31, file =“X23”, status =“old”, action =“read”, & access =“direct”, recl =720)  See program data_transfer.f !

17 I / O positioning INPUT POSITIONING  Let’s consider an existing file has been connected to unit 7 for sequential access. A rewind initial position property for the connection was specified in the open statement. Suppose that several read statements are then executed. 1) If the file is unformatted, each read will advance the file by one record. 2) If the file formatted, each advancing read statement will advance the file by one record. 3) Each non advancing read from a formatted file will advance the file when a slash is encountered in the format or when rescan occurs, and the position remains within the current record at the end of execution of the statement.  Let us suppose that the end-of-file indicator is encountered. The file is then positioned following the end-of-file indicator, and indication of the end-of-file condition is returned to the program.

18 I / O positioning OUTPUT POSITIONING  Now let’s consider a file that has to be used for sequential output. It is connected to unit 13 and contains no records, no indication of the end- of-file condition. A rewind initial position property for the connection was specified in the open statement. Suppose that several write statements are then executed. 1) If the file is unformatted, each write will add one record to the file. 2) If the file formatted, each advancing write will add at least one record. 3) A non advancing write will add a new record when a slash is encountered in the format or when rescan occurs, but will not end the record after data transfer.  After the desired data has been transmitted to the file, the statement end file may be executed to append an end-of-file indicator following the last record written.

19 Direct access input and output The connection of a file to a unit has an access method, which is sequential or direct. Direct access means that records may be accessed in an arbitrary sequence. Each record of the file has a unique record number that is established when the record is written and does not change. A record number specifier in the control list of each read or write statement indicates which record is to be read or written; this specifier has the form rec = record number Record number is an integer expression with a positive value. Example for direct access output statement: write (unit = 3, fmt = *, rec = 76) X, Y, Z, Z5  Write statement sends the information to the record number 76 of unit 3.

20 Unformatted input and output  The connection of a file to a unit has a form which is formatted or unformatted. Unformatted files usually correspond to so-called binary files, whereas formatted files correspond to text files.  Unformatted records contain information in a processor-dependent form that is obtained by transferring data to and from internal storage without any editing or other transformation. The external representation of the data corresponds exactly to its internal representation. Thus, unformatted data transfer is more efficient because the data is merely copied between the external device and the internal storage of the computer.  Formatted records are composed of characters representing data values; a formatted record corresponds to a printed line. Conversion is required between the internal form of the data and its representation on the file.  Do example 9.10 (Meissner’s book) !

21 formatted internal data transfer  Formatted internal data transfer changes the representation of data without actually transmitting it to or from an external device. Formatted external data transfer includes two separate processes: a) The change in representation of data between its internal form and its external form. b) The transmission of data to or from the external device.  In an internal data transfer statement, the integer expression or asterisk unit specifier in the control list is replaced by the name of a character variable that serves as a buffer or “internal file”.  Do example 9.11 (Meissner’s book) !

22 Exercise Ellis’s Book  Study Chapter 10