Fortran Read Last Line From File

Fortran Read Last Line From File Average ratng: 3,5/5 5297 votes
  1. Sed Remove Last Line From File
  2. Give More Feedback

FORTRAN I/O FORTRAN: Input/Output (I/O) SIMPLE I/O: LIST-DIRECTED The simplest form of the I/O statement is the list-directed form which is represented by: READ(.,.) item1, item2, item3. WRITE(.,.) item1, item2, item3. Where ITEMx = a variable, a constant or math expression Example: WRITE(.,.) 'ALPHA=', ALPHA The first asterisk (.) means the input comes from the keyboard in a READ statement and goes to the screen in a WRITE statement. The second asterisk (.) means the computer decides how the I/O elements should look based on the TYPE of data in the input/output list.

  • Handling End-of-File: the READ Statement Revisited. In many situations, you really do not know the number of items in the input. It could be so large to be counted.
  • Fortran's file reading counts a null line as a valid record so fortunately. The next function reads n lines in the file and displays the last read if possible.

Standard FORTRAN reserves two UNIT. SCORE2 100 format(A10,1x,f6.1,1x,f6.2) read this one line from input file, unit=10. A10) output file (unit=20) line is: Last.

This is sometimes called 'FREE-FORMAT'. NOTES ON LIST-DIRECTED I/O SPACES may be inserted between components of the statement to add clarity. The READ statement causes the program to PAUSE and allow you to enter values.

The program will not continue until all values have been entered. Separate values with SPACES when typing data into the program with a READ operation. Make sure you press the ENTER key.

Insert a WRITE statement to PROMPT yourself for input just before a READ statement. This statement should tell the user WHAT to enter. GENERAL FORM OF I/O STATEMENTS The general form of the FORTRAN I/O statements allow data transfer to FILES, TAPE, PRINTER and other devices as well as the TERMINAL. The general form is: WRITE(unit#, format, options) item1, item 2. READ(unit#, format, options) item1, item2.

NOTE: We will restrict our use to TERMINAL and FILE I/O. Typically, you transfer data files to/from tape, diskette, and printer by using UNIX (or DOS on PCs) commands, rather than a Fortran program reading or writing directly from/to the device. In this form, PARENTHESES are used to enclose information about the UNIT, the FORMAT (if any) and other options. Again, SPACES may be used to add clarity to the statement. The input and output lists (item1, item2.) are composed of constants, variables or expressions, separated by COMMAS.

UNIT NUMBER The UNIT is a number which has an association with a particular device. The device can be the TERMINAL or a FILE (or something else too). The UNIT is an INTEGER or INTEGER EXPRESSION, generally between 1-30. Standard FORTRAN reserves two UNIT numbers for I/O to user. They are: UNIT = 5 for INPUT from the keyboard with the READ statement UNIT = 6 for OUTPUT to the screen with the WRITE statement Most versions of FORTRAN will also let you use the ASTERISK (.) for I/O to the TERMINAL. The asterisk can be used with both the READ and WRITE statements, thus there is no need to remember whether 5 or 6 is for input or output.

When I/O is to a file you must ASSOCIATE a UNIT number (which you choose) with the FILENAME. Use any unit number other than 5 and 6. On some computers, some unit numbers are reserved for use by the computer operating system.

The association of the unit number and filename occurs when the OPEN statement is executed. OPEN(UNIT=n, FILE='filename', options.) This statement associates UNIT n with the file mentioned. All subsequent READs or WRITEs using unit n will be to or from this file. MISCELLANEOUS FILE I/O NOTES When doing I/O to a FILE, each READ statement inputs data from a NEW LINE and each WRITE statement outputs data on a NEW LINE. Most files are SEQUENTIALLY organized. You can't easily go back up in the file, but you can REWIND the file with: REWIND unitnumber When you are finished with the file, you may CLOSE it with: close(10) or close(unit=10) (This is optional.) FORMAT IDENTIFIER The FORMAT IDENTIFIER as used in a WRITE or READ statement generally has one of two forms;.

An asterisk (.), indicates LIST-DIRECTED or 'free format'. A LABEL designates a FORMAT statement which specifies the format to use. For simple I/O, the. is used as we have already discussed.

When you want to FORMAT your I/O, you will generally use method 2. The FORMAT statement is a list of FORMAT DESCRIPTORS separated by commas which describe what each FIELD of the output should look like. Example: WRITE(.,10) 'USING', L2, 'AREA =', AREA 10 FORMAT(1X, A5, 2X, I3, 4X, A6, 2X, F6.2) In a format statement, there is a one-to-one correspondence with the items in the I/O list, with the exception of certain positional descriptors; X, T, /. The FORMAT statement is defined only once (for each label referenced) in the program but may be used by any number of I/O statements. FORMAT DESCRIPTORS - The elements in the I/O list must agree in TYPE with the FORMAT DESCRIPTOR being used. There are a dozen or so descriptors, The most commonly used are: Descriptor Use rIw Integer data rFw.d Real data in decimal notation rEw.d Real data in scientific notation Aw Character data 'x.x' Character strings nX Horizontal spacing (skip spaces) / Vertical spacing (skip lines) Tc Tab Where: w = positive integer specifying FIELD WIDTH r = positive integer REPEAT COUNT d = non-negative integer specifying number of digits to display to right of decimal. X = any character n = positive integer specifying number of columns c = positive integer representing column number NOTES ON DESCRIPTORS: - Values for I, F, and E descriptors will be right justified.

You must leave enough space for negative sign in the field width for I, E, F, and for decimal point for E, F and for exponent for E. When using the E descriptor, the number will be NORMALIZED.

This means the number is shifted over so that the first digit is in the tenths position. A leading zero and decimal are always present and a minus sign, if needed. The exponent always requires 4 spaces; E, the sign and 2 digits. If you overflow the space allotted to a descriptor, it will print out asterisks (at RUN TIME) in the field space.

Character data (A) is also right-justified but if it overflows the allotted space it is left-most truncated. The first character of each line of output is used to control the vertical spacing.

Use 1X to give normal spacing. MORE COMPLICATED EXAMPLES: 1. Format identifier Given: REAL ROOT1, ROOT2 1. Unformatted or 'free format' output: All three are equivalent Print., root1, root2 Write (6,.) root1, root2 Write (6,.) 'Roots are:' root1, root2 2.

File

Sed Remove Last Line From File

Formatted write statements: Write (6,100) root1, root2 100 format (1x, 'The Roots Are: ', F5.1,1x,F5.1) output: The Roots Are: 123.5 789.1 3. Write(6,'(',The Roots Are: ', 2F5.1)') root1,root2   4. Define character.5 FRMT FRMT = '2F5.1' write(6,FRMT) root1, root2 2. Format Descriptors Integer: Iw examples: NUM1 = 123, NUM2 = 456, NUM3 =9 write(6,15) NUM1, NUM2, NUM3 15 format (1x,I3,1x,I3,1x,I3) output: 123 456 9 ^ ^ ^^^ write(6,18) NUM1, NUM2, NUM3 18 format (1x,3(I2,1x)) output:. Note: If the number is too large ^ ^ ^^ for #spaces allotted,.

are printed. Reals: REAL NUMBER1, NUMBER2 NUMBER1 = -123.5678 NUMBER2 = -23456.89 A.

F format (decimal) Fw.d d=# places to right of decimal w-d or = 2 (to include the decimal pt and +/- sign) NUMBER1 needs format F9.4 NUMBER2 needs format F9.2 B. E format - good for 1) large or small numbers 2) don't know the size of number Ew.d w - d or = 7: need at least 7 places for: e-03 - 4 places +/- - 1 place 0. 2 places NUMBER1 needs format E14.7 -0.1235678E+03 NUMBER2 need foprmat E14.7 -0.2345689E+05 C. G-format G10,3 chooses between E & F format depending on the size of the number D.

This is my problem: I would like to read data (saved in a text file) from a file. My data is written in a matrix format (1,000,000 rows x 3 columns). I want to read a data from a particular line, say row number 90,000. Since the number of rows is large, it will be very expensive if I have to do an empty read for 89,999 rows. Is there a way I can directly go to row number 90,000 (without reading the lines before it) and read the corresponding data? I do have control over how the text file is created. Will really appreciate any help or advice on this.

Thank You, SC. This is my problem: I would like to read data (saved in a text file) from a file. My data is written in a matrix format (1,000,000 rows x 3 columns). I want to read a data from a particular line, say row number 90,000. Since the number of rows is large, it will be very expensive if I have to do an empty read for 89,999 rows. Is there a way I can directly go to row number 90,000 (without reading the lines before it) and read the corresponding data? I do have control over how the text file is created.

Give More Feedback

Will really appreciate any help or advice on this.I can't imagine that this would work very well for you. You're trying to perform random access on an object whose normal mode of reading is sequential access. Although it might seem that the data in your file is in matrix form, in reality, the data is in 1,000,000 million lines. It might be more realistic to read the data from the file, and store it in memory in an actual matrix (a two-d array).

You didn't say what kind of data, so I can't say how much memory the matrix would use. If each line in your file consists of three double precision numbers, (at eight bytes each), the matrix would use about 24,000,000 bytes, or about 24 MB. It would take a while to read the data into memory, but once there, you could access any element in the 90,000 row pretty quickly.