Reputation: 31
I'm having trouble getting a format string to be accepted by a read statement in a Fortran program, compiled by gfortran under OS X.
The read statement and string constitute two lines of code, and the various error messages I have received appear to depend on how these two statements are used in parallel (plus, just in case, the declaration of the array that is to be read into):
read(10, format) ( velmatt(n,row,i),m=1,3 )
format = "(11x,' x ',a3,2x,i8,6x,3f11.6)"
This results in the error,
Fortran runtime error: Missing initial left parenthesis in format
I must add that I have also tried alternative a few alternative syntax for the format statement and it's call by the read function, since the exact form recommended seems to vary (I am new to fortran). Here are some alternatives,
read(10, 11) ( velmatt(n,row,i),m=1,3 )
11 format(11x,' x ',a3,2x,i8,6x,3f11.6)
Or even
read(10, 'format') ( velmatt(n,row,i),m=1,3 )
format = (11x,' x ',a3,2x,i8,6x,3f11.6)
At least one of these gives an error
Fortran runtime error: Constant string in input format
And one of the two also gives this error,
Missing format label at (1)
I read that the format statement should be parantheses enclosed by quotes, but in that case my first approach should work? The error messages thus seem to be complementary to each other and there is something else I'm missing..
(I'm also not yet clear on the significance of the correct unit numbers to use in fortran so sorry if that is part or all of the problem)
Anyway, none of the above (plus maybe more that I have tried) satisfy the compiler.
Upvotes: 1
Views: 2160
Reputation: 7267
Of the examples you posted, only the one with
read (10,11)
is correct. In the first, you are asking the READ to look for the format in a variable named FORMAT, which I assume you declared as CHARACTER earlier. If you had assigned the format first, it probably would have worked, but this is not the recommended way to do it. But as it was, the variable was uninitialized and did not have the correct value.
You can either use a labeled format as you did with the (10,11) case, or you can put the format itself in the READ like this:
read(10, "(11x,' x ',a3,2x,i8,6x,3f11.6)") ...
Upvotes: 3