1 Chapter 8: File Processing Part 1. 2 Outline  Why Files?  Opening Files  Reading from Files  Writing to Files  Working with Multiple Files  Closing.

Slides:



Advertisements
Similar presentations
Exercise (1).
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
Chapter 3 Reading a Set of Data. CIS 1.5 Introduction to Computing with C++3-2 Reading a Set of data A Simple Payroll Program Problem: Write a complete.
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.
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the.
CS Sept 2006 Pick ups from chapters 4 and 5.
CS150 Introduction to Computer Science 1
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
1 Chapter 2: Data Types & Operations Part 3 ICS101: Computer Programming Al-Hashim, Amin G.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
ALGORITHMS AND FLOWCHARTS
More While Loop Examples CS303E: Elements of Computers and Programming.
You can select between blocks of statements by using the selection construct IF statement is used as a selection construct Four types of IF constructs.
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.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
Previously Repetition Structures While, Do-While, For.
Chapter 4: Control Structures II
Count and add list of numbers From user input and from file.
Control Structures RepetitionorIterationorLooping Part I.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
LAB-09 DO WHILE loop I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS1010: Programming Methodology
For loop. Exercise 1 Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Chapter 5: Control Structure
Chapter 4: Control Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 07 More Repetition Richard Gesick.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
For Monday Read WebCT quiz 18.
Logical Operators and While Loops
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Programming Funamental slides
Chapter (3) - Looping Questions.
ALGORITHMS AND FLOWCHARTS
For Wednesday No new reading No quiz.
Writing Functions( ) (Part 4)
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Logical Operators and While Loops
FILE PROCESSING Opening Files Why Files?
Repetition (While Loop) LAB 9
EECE.2160 ECE Application Programming
DATA TYPES AND OPERATIONS
EECE.2160 ECE Application Programming
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

1 Chapter 8: File Processing Part 1

2 Outline  Why Files?  Opening Files  Reading from Files  Writing to Files  Working with Multiple Files  Closing Files  Rewinding Files

3 Why Files?  Huge data  Repetitive data is needed for more than one program  Data may be entered by other people or instruments

4 Opening Files  Preparing a file for input:  Preparing a file for output: OPEN(UNIT = INTEGER EXPR, FILE = FILENAME, STATUS = 'OLD') OPEN(UNIT = INTEGER EXPR, FILE = FILENAME, STATUS = 'NEW') OPEN(UNIT = INTEGER EXPR, FILE = FILENAME, STATUS = 'UNKNOWN') [0-99]-{5,6}

Cont. Example 1 Assume that you want to use the file POINTS.DAT as an input file. The following statement will then appear before any read statement from the file: OPEN(UNIT = 1, FILE = 'POINTS.DAT', STATUS = 'OLD') 5 Example 2 Assume that you want to use the file RESULT.DAT as an output file. The following statement will then appear before any write statement to the file: OPEN(UNIT = 1, FILE = 'RESULT.DAT', STATUS = 'UNKNOWN')

6 Reading from Files  Form 1: READ(UNIT, *) VARIABLE LIST

77 Cont. ? Find the sum of 3 exam grades taken from the EXAM.DAT file. INTEGER EXAM1, EXAM2, EXAM3, SUM OPEN(UNIT = 10, FILE = 'EXAM.DAT', STATUS = 'OLD') READ(10, *) EXAM1, EXAM2, EXAM3 SUM = EXAM1 + EXAM2 + EXAM3 PRINT*, SUM END

8 Cont.  Form 2: READ(UNIT, *, END = NUMBER) VARIABLE LIST

99 Cont. ? Find the average of real numbers that are stored in the file NUMS.DAT. Assume that we do not know how many values are in the file and that every value is stored on a separate line. REAL NUM, SUM, AVG INTEGER COUNT OPEN(UNIT = 12, FILE = 'NUMS.DAT', STATUS = 'OLD') SUM = 0.0 COUNT = 0 3READ(12, *, END = 9) NUM SUM = SUM + NUM COUNT = COUNT + 1 GOTO 3 9AVG = SUM / COUNT PRINT*, ‘AVERAGE =‘, AVG END

10 Writing to Files  Form: WRITE(UNIT, *) EXPRESSION LIST

11 Cont. ? Create an output file CUBES.DAT that contains the table of the cubes of integers from 1 to 20 inclusive. INTEGER NUM OPEN(UNIT = 20, FILE = 'CUBES.DAT', STATUS = 'UNKNOWN') DO 22 NUM = 1, 20 WRITE(20, *) NUM, NUM**3 22CONTINUE END

12 Working with Multiple Files ? Create an output file THIRD that contains the values in file FIRST followed by the values in file SECOND. Assume that every line contains one integer number and we do not know how many values are stored in files FIRST and SECOND. INTEGER NUM OPEN(UNIT = 15, FILE = 'FIRST', STATUS = 'OLD') OPEN(UNIT = 17, FILE = 'SECOND', STATUS = 'OLD') OPEN(UNIT = 19, FILE = 'THIRD', STATUS = 'UNKNOWN') 33READ(15, *, END = 66) NUM WRITE(19, *) NUM GOTO 33 66READ(17, *, END = 30) NUM WRITE(19, *) NUM GOTO 66 30STOP END

13 Closing Files  Form: CLOSE (UNIT)

14 Rewinding Files  Form: REWIND (UNIT)

15 Cont. ? Given a data file ‘INPUT.DAT’ that contains unknown number of lines. Each line has a student ID and his grade. Write a program that reads the data from the above file and writes to a new file ‘OUTPUT.DAT’ in each line the ID and the grade of all the students who have grades greater than the average. REAL GRADE, SUM, AVG INTEGER ID, COUNT, K OPEN (UNIT = 20, FILE = ‘INPUT.DAT’, STATUS = ‘OLD’) OPEN (UNIT = 21, FILE = ‘OUTPUT.DAT’, STATUS = ‘UNKNOWN’) SUM = 0.0 COUNT = 0 44READ (20, *, END =100 ) ID, GRADE SUM = SUM + GRADE COUNT= COUNT+ 1 GOTO AVG = SUM/ COUNT REWIND (20) DO 50 K = 1, COUNT READ (20, * ) ID, GRADE IF (GRADE. GT. AVG) WRITE (21, *) ID, GRADE 50CONTINUE END

16 Exercise 1 What will be printed by the following program? INTEGER M, K OPEN ( UNIT = 10, FILE = 'INPUT.DAT', STATUS = 'OLD') READ ( 10, *, END = 10) ( M, K = 1, 100) 10PRINT*, M, K - 1 END INPUT.DAT

17 Exercise 2 What will be printed by the following program? INTEGER J, K OPEN ( UNIT = 3, FILE = 'FF1.DAT', STATUS = 'OLD') DO 50 J = 1, 100 READ ( 3, *, END = 60) K 50CONTINUE 60PRINT*,'THE VALUES ARE:' PRINT*, K, J END FF1.DAT

18 Exercise 3 What will be printed by the following program? INTEGER M OPEN ( UNIT = 10, FILE = 'INPUT.DAT',STATUS = 'OLD') READ (10,*) M 20IF ( M.NE. -1) THEN PRINT*, M READ(10, *, END = 30) M GOTO 20 ENDIF PRINT*, 'DONE' 30PRINT*, 'FINISHED' END FF1.DAT

19 Exercise 4 What will be printed by the following program? INTEGER A, B OPEN ( UNIT = 10, FILE = 'INPUT.DAT', STATUS = 'OLD') OPEN ( UNIT = 11, FILE = 'OUTPUT.DAT', STATUS = 'NEW') READ*,A, B READ(10, *) A, B, A WRITE(11,*) A, B READ(10, *, END = 10) A, B 10WRITE(11, *) A, B END FF1.DAT USER