09.01.2020

Fortran Read Last Line From File

61

Handling End-of-File: the READ Statement Revisited Handling End-of-File: the READ Statement RevisitedIn many situations, you really do not know the number of items in theinput. It could be so large to be counted accurately. Consequently,we need a method to handle this type of input. In fact, you have encounteredsuch a technique in Programming Assignment 1 in which a keywordIOSTAT= was used in a READ statement. The following isits syntax:INTEGER:: IOstatusREAD(.,.,IOSTAT=IOstatus) var1, var2., varnThe third component of the above READ is IOSTAT= followedby an INTEGER variable. The meaning of this new form of READis simple:After executing the above READ statement, the Fortran compiler willput an integer value into the integer variable following IOSTAT=,IOstatus above. Based on the value of IOstatus, we have threedifferent situations:.

If the value of IOstatus is zero, the previous READwas executed flawlessly and all variables have received theirinput values. This is the normal case. If the value of IOstatus is positive, the previousREAD has encountered some problem. In general, withoutknowing the system dependent information, it is impossible todetermine what the problem was.

However, if hardware and I/Odevices are working, a commonly seen problem would be illegaldata. For example, supplying a real number to an integervariable. If IOstatus is positive, you cannot trustthe values of the variables in the READ statement;they could all contain garbage values, or some of them are finewhile the others are garbage.

Fortran Read String From File

If the value of IOstatus is negative, it means the end ofthe input has reached. Under this circumstance, some or all of thevariables in the READ may not receive input values.What is the end of file?

Write

Fortran Read Line

How do we generate it?If you prepare your input using a file, when you save it,the system will generate a special mark, calledend-of-file mark, at the end of that file. Therefore,when you read the file and encounter that special end-of-file mark, thesystem would know there is no input data after this mark. If you try toread passing this mark, it is considered as an error.If you prepare the input using keyboard, hiting the Ctrl-D key wouldgenerate the end-of-mark under UNIX. Once you hit Ctrl-D, thesystem would consider your input stop at there. If your program tries toread passing this point, this is an error.However, with IOSTAT=, you can catch this end-of-file mark anddo something about it. A commonly seen application is that let the programto count the number of data items as will be shown in examples below.Examples.

Fortran Read Last Line From File To Pdf

In the following code, the DO-loop keeps reading in threeinteger values into variables a, b and c.After executing a READ, if Reason is greater thanzero, something was wrong in the input; if Reason isless than zero, end-of-file has reached. Only ifReason is zero, one can start normal processing.INTEGER:: ReasonINTEGER:: a, b, cDOREAD(.,.,IOSTAT=Reason) a, b, cIF (Reason 0) THEN. Something wrong.ELSE IF (Reason 0) THENWRITE(.,.) 'Check input. Something was wrong'EXITELSE IF (io.