Fortran allows you to read data from, and write data into files. In the last chapter, you have seen how to read data from, and write data to the terminal. (I do not want to read the whole file, just last several lines) So, I wrote a subroutine that should do this for me (see code further). It reads the last line without a problem. At the beginning of the first line, the program crashes. So Intel fortran compiler does not obey the rule. In this chapter you will study file input and output functionalities provided by Fortran. You can read and write to one or more files. The OPEN, WRITE, READ and CLOSE statements allow you to achieve this. Opening and Closing Files Before using a file you must open the file. The open command is used to open files for reading or writing. The simplest form of the command is − open (unit = number, file = 'name'). Simple serial pic programmer online. However, the open statement may have a general form − open (list-of-specifiers) The following table describes the most commonly used specifiers − Sr.No Specifier & Description 1 [UNIT=] u The unit number u could be any number in the range 9-99 and it indicates the file, you may choose any number but every open file in the program must have a unique number 2 IOSTAT= ios It is the I/O status identifier and should be an integer variable. If the open statement is successful then the ios value returned is zero else a non-zero value. 3 ERR = err It is a label to which the control jumps in case of any error. 4 FILE = fname File name, a character string. 5 STATUS = sta It shows the prior status of the file. A character string and can have one of the three values NEW, OLD or SCRATCH. A scratch file is created and deleted when closed or the program ends. 6 ACCESS = acc It is the file access mode. ![]() Can have either of the two values, SEQUENTIAL or DIRECT. The default is SEQUENTIAL. 7 FORM = frm It gives the formatting status of the file. Can have either of the two values FORMATTED or UNFORMATTED. The default is UNFORMATTED 8 RECL = rl It specifies the length of each record in a direct access file. After the file has been opened, it is accessed by read and write statements. Once done, it should be closed using the close statement. ![]() The close statement has the following syntax − close ([UNIT = ]u[,IOSTAT = ios,ERR = err,STATUS = sta]) Please note that the parameters in brackets are optional. Example This example demonstrates opening a new file for writing some data into the file. Program outputdata implicit none real, dimension(100):: x, y real, dimension(100):: p, q integer:: i! Data do i=1,100 x(i) = i * 0.1 y(i) = sin(x(i)) * (1-cos(x(i)/3.0)) end do!
0 Comments
Leave a Reply. |