2

I am following a Fortran tutorial, however the below code does not seem to work. I am trying to read a 2 digit number from command line, however I get the error UNIT specification at "TRIM(buffer)" must be an INTEGER expression or a CHARACTER variable.

INTEGER :: number
CHARACTER(LEN=20) :: buffer

buffer = ""
CALL GET_COMMAND_ARGUMENT(1, buffer)
READ(TRIM(buffer), FMT="(I2)") number

So the issue as I understand it, is that READ is not sure that the output of TRIM will return a CHARACTER value. Can I somehow declare that it is a CHARACTER or is there another way of getting around this issue?

For what it is worth I am using gfortran.

1 Answer 1

3

TRIM(buffer) indeed returns a character value. This is, however, an expression and not a variable. As the error message suggests, an expression is fine for giving a unit number, but in the case of reading from a character, a variable is required.

As TRIM will act merely to provide a value with the trailing spaces stripped from buffer, this isn't needed: the spaces won't affect the reading. Instead, just go for

read(buffer, '(I2)') number

or even

read(buffer(1:2), '(I2)') number

More generally, a more complicated character expression can be assigned to a variable and this latter variable used in the read context.

Sign up to request clarification or add additional context in comments.

Comments

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.