Please explain this format
Please explain this format
This is a fairly typical Fortran format. The meaning is as follows:
- 80 means that the edit descriptor that follows is to repeated 80 times.
- The edit descriptor is A1 - A is for characters and the 1 means that one single character is to be consumed (in reading) or printed (in writing)
So the format can be used to read 80 characters from one line of input and store these individual characters in as many strings. Or to print up to 80 characters taken from the variables that appear in a WRITE statement.
The actual working is best investigated in a small sample program - easier than writing out all the rules (and text books are much better in that than posts of this type anyway ;)).
As Arjen has already noted, it is best for you to show the corresponding I/O-list that is to be used with the format statement in a READ or WRITE statement, and the type declarations of the variables.
You can also describe what you want the I/O statement to do (if you already know), and the responses would then advise you accordingly.
Instead of posting your question piecewise across several posts, please post a coherent and complete description in one place.
print '(A10)', "ABCD" print '(10A1)', "ABCD" print '(10A1)', "A", "B", "C", "D"
ABCD A ABCD
I still dont understand the following:
SUBROUTINE Test CHARACTER*1 Char(100) READ (1, 100, END=50, ERR=50) Char blah blah blah blah blah blah blah blah 50 RETURN 100 FORMAT (100A1)
When the READ starts, does it proceed to “blah blah” ?
or it first finishes all the 100 character inputs AND THEN it proceeds to “blah blah” ?
Does READ → FORMAT work like a loop here ?
I added some comments to the code to explain what it does. There is no loop, since the single READ statement should get all the data at once.
(Side comment – maybe the coloring of code comments should be turned off.)
SUBROUTINE Test CHARACTER*1 Char(100) ! declares an array of 100 length-1 character variables READ (1, 100, END=50, ERR=50) Char ! reads those character variables from unit 1 using format statement 100 ! at the end of the subroutine. If there is a problem doing so, branch to line 50 ! to exit the subroutine and skip blah blah blah blah blah blah blah blah blah blah 50 RETURN 100 FORMAT (100A1) ! format for reading up to 100 length-1 character variables
Here is a modernized program that uses the same features.
program xread_char implicit none integer, parameter :: nlen = 5 character (len=1) :: s(5) character (len=nlen) :: text integer :: i text = "01234" read (text,"(5a1)") s print "(a)",(s(i),i=1,nlen) end program xread_char ! output: ! 0 ! 1 ! 2 ! 3 ! 4
READ (1, 100, END=50, ERR=50) Char 100 FORMAT (100A1)
read 100 values from the file altogether (1 character by 100 times)
and store them in the array “char”, 1 by 1
until you reach 100 characters OR error OR end of file
and after you store this values to the array, continue the “blah blah” instructions.
did i get it right ?
Yes, that’s right.
If you are modifying the program I would look at the section following the READ to see if the data is better stored and processed as a single
character (len=100) variable.
thanks for your help
I m actually trying to transfer a FORTRAN 77 program to another language
the code is actually a file reader
it reads 100 characters a time from a file
and then it goes char after char in half a dozen loops to check if they re valid words or not
for example the file has:
“%67 #25 load filename Beliavsky”
and the program determines if there s a load or a save or a name or a file buffer etc etc
I guess it uses these loops because there are no records of standard length
Anyway, thanks again for your help