Carlo
Carlo

Reputation: 1578

Attach strings in filename

Now I have something like this

program prova
CHARACTER (LEN=4) :: mvalue
common mvalue   
mvalue='01.0'
call funzione(var1, var2,...)
end

subroutine funzione()
common mvalue
*(stuff with var1, var2, ...)*
open(10,file="./prova_"//mvalue//"_.res")
end

and the compiler returns

 open(10,file="./prova_"//mvalue//"_.res")                         
                                        1
 Error: Operands of string concatenation operator at (1) are CHARACTER(1)/INTEGER(4)

I don't know if I can use the "implicit none" instruction in the real code, because maybe it would mess up something else...I'm sorry if I can't be more precise, but as I told you I'm new to FORTRAN, and the code is kinda extended (and written EXTREMELY poorly). I'd prefer to pass the mvalue variable to the routine, but if I try to do

program prova
CHARACTER (LEN=4) :: mvalue
mvalue="01.0"
call func(mvalue)
end


subroutine func(mvalue)
open(10,file="./prova_"//mvalue//"_.res")
end

it returns

open(10,file="./prova_"//mvalue//"_.res")                         
                           1
Error: Operands of string concatenation operator at (1) are CHARACTER(1)/INTEGER(4)

prova.f:4.16:
call func(mvalue)                                                 
Warning: Type mismatch in argument 'mvalue' at (1); passed CHARACTER(1) to INTEGER(4)

Upvotes: 0

Views: 1859

Answers (2)

Your subroutine:

subroutine funzione()
common mvalue
*(stuff with var1, var2, ...)*
open(10,file="./prova_"//mvalue//"_.res")
end

is missing implicit none ! Always use it in every scoping unit without any exception!

The type of mvalue is not declared and it is therefore integer implicitly.

You must declare the type in every scoping unit. Sharing it using the common is not enough!

In Fortran 90 the proper thing is to use modules and not common blocks. In any programming language in general the proper way is not to share global variables (no matter if using common, using modules or any other way), but to pass the string as an argument to the subroutine.


To your new edit the same advice is still valid: Your subroutine is missing implicit none ! Always use it in every scoping unit without any exception!

If you can't use implicit none then you MUST!!!!! be prepared to diagnose errors coming from undeclared variables. It is absolutely necessary!

A character dummy argument is declared as character*(*) mvalue

Upvotes: 2

chw21
chw21

Reputation: 8140

As the commenters have already pointed out, you are using two different quotation markers in such a way that it passes the compile time, but fails at runtime.

The file= dummy argument expects a string, and it gets a very long one: The entirety of ./results/file_01.0_R00.res',status='unknown. But when it tries to open that file, it fails: This is simply no valid file name.

Simply replacing the ' with " in both instances (after .res and before unknown) should make the error go away.

Upvotes: 0

Related Questions