Fortran File Processing: Reading and Writing Data Files, Lab Reports of Computer Science

How to use files for input and output in fortran programming. It covers the three steps for file processing: opening, reading or writing data, and closing the file. The document also includes a sample program that reads real numbers from an input file, rounds them to the nearest integers, and writes the integers to an output file.

Typology: Lab Reports

Pre 2010

Uploaded on 08/30/2009

koofers-user-90q-1
koofers-user-90q-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fortran Programming
CSCE 150 Lab 7
File Processing
1. File Processing
2. Sample Program
3. Exercise for Credit
1. File Processing
Sometimes it is convenient in a Fortran program to use files for accessing or storing data,
especially when large amounts of data are involved. Too much keyboard input during the
run of a program leads to mistakes and tedium, while too much screen output has similar
consequences. Putting data into files - both for input and output - is a more leisurely and
less error-prone approach.
There are three steps for using files in a Fortran program:
1. Open a file for read or write. The file used for writing may be either new or a
replacement of an existing file.
2. Read or write data to the file.
3. Close the file.
Note: When you use a file for reading, you need to create this file if it does not exist.
Step 1: Open
Opening a file for input
Open (UNIT=unit_number, FILE=filename, STATUS='OLD', &
ACTION='READ', IOSTAT=ierror)
Opening a file for output
Open (UNIT=unit_number, FILE=filename, STATUS='NEW ', &
ACTION='WRITE', IOSTAT=ierror)
Open (UNIT=unix_number, FILE=filename, STATUS='REPLACE', &
ACTION='WRITE', IOSTAT=ierror)
unit_number is and integer used to uniquely identify the file.
filename is the name (including the path) of the file.
STATUS specifies the status of the file:
oOLD: file already exists (used for file reading).
oNEW: file does not exist (used for creating a new file without
overwriting any file with the same name)
oREPLACE: Used for creating a new file. If another file with the
same name exist, then overwrite the existing file.
ACTION specifies the purpose of file opening (reading or writing).
pf3
pf4

Partial preview of the text

Download Fortran File Processing: Reading and Writing Data Files and more Lab Reports Computer Science in PDF only on Docsity!

Fortran Programming

CSCE 150 Lab 7

File Processing

**1. File Processing

  1. Sample Program
  2. Exercise for Credit**

1. File Processing

Sometimes it is convenient in a Fortran program to use files for accessing or storing data, especially when large amounts of data are involved. Too much keyboard input during the run of a program leads to mistakes and tedium, while too much screen output has similar consequences. Putting data into files - both for input and output - is a more leisurely and less error-prone approach. There are three steps for using files in a Fortran program:

  1. Open a file for read or write. The file used for writing may be either new or a replacement of an existing file.
  2. Read or write data to the file.
  3. Close the file. Note: When you use a file for reading, you need to create this file if it does not exist. Step 1: Open Opening a file for input Open (UNIT=unit_number, FILE=filename, STATUS='OLD', & ACTION='READ', IOSTAT=ierror) Opening a file for output Open (UNIT=unit_number, FILE=filename, STATUS='NEW ', & ACTION='WRITE', IOSTAT=ierror) Open (UNIT=unix_number, FILE=filename, STATUS='REPLACE', & ACTION='WRITE', IOSTAT=ierror)  unit_number is and integer used to uniquely identify the file.  filename is the name (including the path) of the file.  STATUS specifies the status of the file: o OLD: file already exists (used for file reading). o NEW: file does not exist (used for creating a new file without overwriting any file with the same name) o REPLACE: Used for creating a new file. If another file with the same name exist, then overwrite the existing file.  ACTION specifies the purpose of file opening (reading or writing).

 ierror is a integer variable used for getting the error code if the open operation fails. (ierror is 0 if the file is opened successfully, and non-zero if an error occurs). Step 2: READ or WRITE READ (UNIT=unit, *, IOSTAT=ierror) WRITE (UNIT=unit, *, IOSTAT=ierror) ierror is an integer that has a non-zero value if the read/write operation fails. Helpful commands for reading: BACKSPACE (UNIT=unit): moves back one record. REWIND (UNIT=unit): restarts the file at its beginning. Step 3: CLOSE Close (UNIT=unit)

2. Sample program: Round the values in a data file to integers The sample program reads an arbitrary number of real numbers from a user-specified input data file, rounds the numbers to the nearest integers, and writes the integers to a user-specified output file. PROGRAM round IMPLICIT NONE ! List of variables: CHARACTER(len=36) :: filename1! Input file CHARACTER(len=36) :: filename2! Output file INTEGER :: istat! I/O Status of READ and WRITE operation INTEGER :: istat1! I/O Status of input file OPEN INTEGER :: istat2! I/O Status of output file OPEN REAL :: value! Input value ! Get the name of the file containing the input data. WRITE (,) 'round -- Round values to nearest integer.' WRITE (,'(1X,A)') 'Enter the input file name: ' READ (,'(A36)') filename ! Get the name of the file to write the output data to. WRITE (,'(1X,A)') 'Enter the output file name: ' READ (,'(A36)') filename ! Open input data file. Status is OLD because the input data must already exist. OPEN ( UNIT=8, FILE=filename1, STATUS='OLD', IOSTAT=istat1 ) ! Is open OK? in_ok: IF ( istat1 /= 0 ) THEN

After executing the program, the output file “output.dat” will be created. Use pico to view the file by typing “> pico output.dat”. The values in “output.dat” should be: 3 5 9

3. Exercise for Credit Exercise 2: Write a program that reads all the real values in a user-specified input data file. Find the largest value in the data file, and write this largest value and the line number on which it was found to a user-specified output file. Note: In Exercise 2, you may use the same input data file created for Exercise 1. However, the two programs should have different output data files. Hints: You may use the following code for finding the maximum value from the file: REAL::maxval REAL::value INTEGER::maxline INTEGER::nline nline = 0 DO READ (8,, IOSTAT = istat) value IF (istat /=0) EXIT nline = nline + IF (nline = = 1) THEN maxval = value maxline = nline END IF IF (maxval < value) THEN maxval = value maxline = nline END IF END DO WRITE (9,,IOSTAT=istat) ‘maxval=’, maxval WRITE (9,* ,IOSTAT=istat ) ‘maxline=’, maxline