1 / 15

LAB-13 File

LAB-13 File. 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

gilon
Download Presentation

LAB-13 File

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LAB-13File I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

  2. 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

  3. 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)

  4. 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

  5. Closing a File • Close a file after no file operation is needed • CLOSE (UNITNO)

  6. 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

  7. 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

  8. 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

  9. 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?

  10. Writing to a File • WRITE (UNIT, *) VARIABLE LIST • The values are written in similar way with PRINT

  11. 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

  12. 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?

  13. 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

  14. 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

  15. 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

More Related