150 likes | 268 Vues
This document provides an overview of file operations in Fortran, explaining how to read from and write to files. You will learn about opening files with various statuses ('OLD', 'NEW', 'UNKNOWN'), performing read and write operations, and how to manage data efficiently. Examples are included to demonstrate how to create, open, and manipulate data files, highlighting the importance of closing files after use. It's a practical guide for handling data with file I/O in real-life applications within the field of computer science.
E N D
LAB-13File I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals
Background • In real life applications we will handle lots of data • The data need to be saved and retrieved for later use • It is cumbersome to retype all the data • Use secondary storage → files
Introduction • Standard input is keyboard • Standard output is screen • File is just another input/output device • The operations are similar • READ • WRITE (like PRINT for screen)
Opening a File • Before using a file, it needs to be opened • OPEN (UNIT = INTEXP, FILE = FILENAME, STATUS = STATSTRING) • INTEXP→ the file handle, 0 – 99 (avoid 5 & 6 → keyboard & screen) • FILE→the file name • STATUS→ 'OLD', 'NEW', 'UNKNOWN' • OLD → existing file • NEW → new file; if file exists, it gives error • UNKNOWN → works for existing file or non-existing file • Multiple files can be opened but use different unit numbers
Closing a File • Close a file after no file operation is needed • CLOSE (UNITNO)
Reading from a File • READ (UNITNO, *) VARIABLE LIST • The values are read in similar way with regular READ • The values are stored in the variables in the list
Example 1 – Prepare the Data • Create a file called DATA1.TXT in D:\ using Notepad • Write the contents of the file as follows 10 20 30 • Open the Fortran IDE for the program
Example 1 – The Program INTEGER X, Y, Z OPEN (UNIT = 1, FILE = 'DATA1.TXT', STATUS = 'OLD') READ (1, *) X, Y, Z PRINT*, X, Y, Z CLOSE (1) END
Notes • Change the status into 'NEW' and run the program. What will happen? • Change the status into 'UNKNOWN' and run the program. What will happen?
Writing to a File • WRITE (UNIT, *) VARIABLE LIST • The values are written in similar way with PRINT
Example 2 – The Program INTEGER X, Y, Z OPEN (UNIT = 1, FILE = 'DATA99.TXT', STATUS = 'NEW') X = 50 Y = 60 Z = 70 WRITE (1, *) X, Y, Z CLOSE (1) END
Notes • Change the file name into 'DATA3.TXT' and the status into 'OLD' and run the program. What will happen? • Change the status into 'UNKNOWN' and run the program. What will happen?
Example 3 – The Program INTEGER X, Y, Z OPEN (UNIT = 1, FILE = 'DATA1.TXT', STATUS = 'UNKNOWN') READ (1, *) X, Y PRINT*, X, Y, Z X = 50 Y = 60 Z = 70 WRITE (1, *) Z, Y, X CLOSE (1) END
Loop READ • What if we do not know the number of values in a file? • READ (UNITNO, *, END = NUMBER) VARIABLE LIST • NUMBER is the line number of the statement where control will be transferred after all the data from the file is read
Example 4 – Loop READ REAL NUM, SUM, AVG INTEGER COUNT OPEN (UNIT = 7, FILE = 'DATA1.TXT', STATUS = 'OLD') SUM = 0 COUNT = 0 • READ (7, *, END = 222) NUM SUM = SUM + NUM COUNT = COUNT + 1 GOTO 111 • AVG = SUM/COUNT PRINT*, AVG END