


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



**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:
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