Presentation on theme: "Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book."— Presentation transcript:
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book
Input and Output From last lecture we used READ and PRINT statements: READ*, AREA, LENGTH. PRINT 7, AREA, LENGTH 7FORMAT(5X,’The Area of the rod is ‘,F8.2,/, + 5X,’The Length of the rod is ‘,F8.2) The READ statement reads the data for the two variables AREA and LENGTH that are input from the keyboard. The PRINT statement will print the values of AREA and LENGTH to the screen along with the text in the format statement shown in the line with address 7. This is an example of list directed input and output
I/O from a Data File In order to use a data file with a program (for input or output) the file must first be opened. This is accomplished with an OPEN statement: OPEN (UNIT=integer expression, File=filename, STATUS=literal) integer expression- designates the unit number to the file. The unit number will be referred to in read or write statements when referring to the file. Some systems reserve specific unit numbers for printers, the keyboard, or the screen. filename- actual filename that you will be using. If this is an input file (data will be read), you need to put the name of the file that you will be using. file status- depends on whether this is going to be an input file or an output file. If the file has been created and the data is to be read from the file you should use STATUS=’OLD’. If data from the program will be written to the file, the program will be creating the file and you should use STATUS=’NEW’. When the file is finished being used by the program it can be closed using the following statement: CLOSE (UNIT=integer expression)
Read/Write You must use the OPEN statement before you read or write to the file. The READ statement does change slightly from the form that we used before and we use a WRITE statement instead of a PRINT statement: READ (unit number,*) variable list WRITE(unit number,*) expression list, or WRITE(unit number,k) expression list. In the first WRITE statement with the * we place the variables and any character strings directly after the WRITE statement. In the latter WRITE statement we refer to a FORMAT statement at address k.
I/O from Files To employ files instead of list directed input: OPEN (UNIT=10, FILE=‘input.ext’,STATUS=‘old’) OPEN (UNIT=11,FILE=‘output.ext’, STATUS=‘new’) READ (10,*) AREA, LENGTH WRITE (11,7) AREA, LENGTH 7FORMAT(5X,’The Area of the rod is ‘,F8.2,/, + 5X,’The Length of the rod is ‘,F8.2) In the above expressions, we first must OPEN the files that we are going to be using for input and output. These files may be opened at any location in the program before we try to read or write to the files. In many cases it is best to OPEN the files near the beginning of the program.
Notes on reading from files Input files should contain lines of data. Each READ statement starts on a new line of data. If a line does not contain enough values to match the number requested in the read statement, the program will begin on the next line. If there are values left on the previous line that were not read, these values will be skipped.
Consider the following data in an input file that has been designated UNIT 10: line 1: line 2: line 3: line 4: If we had the following lines in our program: REAL AREA1, LGTH1, MASS1 READ(10,*) AREA1, LGTH1, MASS1 READ(10,*) AREA2, LGTH2, MASS2 The values that will be read are as follows: AREA1=0.1,LGTH1=10.1, MASS1 = 0.2 AREA2=0.5, LGTH2=10.5, MASS2=1.1 Since there are only two values per line, the program will read MASS1 from the second line. The value of 11.5 is not read since the READ statement is only asking for 3 values. When the second read statement is issued, the program begins on the next line (line 3) of the data file and will start with 0.5.
Reading Data Files When reading from a data file it important to know certain information about the file, such as the name and what information is contains. There are three different ways to read values from data files, depending on what information is known: 1.Use a DO loop when the number of values are known 2.Use a while loop when the last value is known or there is a trailer signal 3.Use a END option when other information is not known
Specified Number of Records If the number of records in a data file is known, a DO loop can be ran that number of times and will read a data value with each pass and store it in the program. ex: DO 2 I= 1,n READ(10,*) X, Y 2 CONTINUE Here unit 10 has n lines of data and 2 columns. The values in the first column were assigned to X and the values in the second column were assigned to Y. After n lines, the program stops reading the file
Trailer Signal When generating a file, a special data value, called a trailer signal, can be added at the end which will signal the program to stop reading the file. It should contain the same number of records as the previous lines. We used a while loop to read the file: ex: trailer signal 999 READ (10,*) A 5 IF(A.NE.999)THEN READ (10,*) A GO TO 5 END IF The first READ statement will read and assign the first value in the file to A. The loop will execute and read a value with each pass as long as the value isn’t 999.
END Option If the number of records isn’t known and there is no trailer signal, an END option can be used, which uses the following form: READ(unit numer,*,END=n) variable list n is the address of a line in the program to go to when there are no more values in the file. ex: 5 READ(10,*,END=15) TIME, TEMP COUNT = COUNT +1 GO TO 5 15 PRINT*