1

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.

5
  • Are you really setting the value of format after referencing it in the read statement? That is, is the first example correctly showing the order of the lines that you use? Commented Jun 9, 2017 at 15:50
  • Does the read work in Absoft Pro Fortran? What options are you passing to the compiler? Commented Jun 9, 2017 at 15:50
  • @francescalus The ordering of the lines is as used in the program, yes, though when I set the format command at the beginning of the program with other initialisations, the error messages are still the same. Commented Jun 10, 2017 at 6:59
  • @MichaelShopsin I'm really not sure - how would I know if I was using Absoft Pro version of fortran? Commented Jun 10, 2017 at 7:00
  • @JSmoov there are a number of Fortran compilers for MacOS X which can behave slightly differently. Also F77 versus F95/99 can change what format statements are accepted. Commented Jun 12, 2017 at 14:23

1 Answer 1

3

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)") ...
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Steve for your suggestion, I've tried both ways but I'm now getting the other kind of error message, "At line 51 of file spectrum.f90 (unit = 10, file = 'fort.10') Fortran runtime error: Constant string in input format (11x,' x ',a3,2x,i8,6x,3f11.6)"
Oh, I missed that. You can't put a constant string in a format used in a READ statement. That's the sort of thing you'd use in a WRITE.
@SteveLionel So, is there a solution for READ?
@JordanHe what kind of solution do you want? You can use a format stored in a character variable in a READ. Or maybe you want to read from a character string? That's called "internal READ", where instead of a unit number you specify a character variable. Perhaps you should open a new question and explain what it is you are looking for.
@SteveLionel What I asked is how to make read(10, "(11x,' x ',a3,2x,i8,6x,3f11.6)") ... work as we expect it to. I have legacy Fortran code that has something exactly like that and the compiler complained Fortran runtime error: Constant string in input format. I found a solution from this link.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.