Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using external variable files 1

Status
Not open for further replies.

byter101

Programmer
Jul 6, 2009
10
US
Hello,
I am dealing with a program that uses a file (called vars.i) which is basically a text file that contains a list of variables (integers and reals) that the program accesses throughout its execution. can anyone explain how this interaction works between the file and the .f90 file? like where are the values stored when they are given values?
 
Do you mean, how to read data from a text file? Post an example of that file.
 
it is just a text file with a list variables declared, for example

!segment
integer*4 :: xloc
integer*4 :: yloc
real(4) :: zaxis

and so on.
I was more just curious how the program interacts with those variables, but from what i understand when you use the 'include' command it automatically reads those variables into memory and treats them as if they were simply included in the code itself.
 
The include statement in Fortran includes the specified source file into your actual source at the time of preprocessing, i.e. short before compilation.
It's good, if you want to use the same piece of code in several source files, for example if you use the same variables in two or more programs. So you declare the variables once and use them in several sources via include.

For example, I have declared my variables in this file
vars.f90
Code:
[COLOR=#2e8b57][b]integer[/b][/color] :: my_integer_var
[COLOR=#2e8b57][b]real[/b][/color] :: my_real_var
and now I want to use the variables declared above in this program
include_example.f90
Code:
[COLOR=#a020f0]program[/color] include_example
  [COLOR=#0000ff]! include source with variable declarations[/color]
  [COLOR=#a020f0]include[/color] [COLOR=#ff00ff]'vars.f90'[/color]

  [COLOR=#0000ff]! use the variables declared in the include file[/color]
  my_integer_var [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]5[/color]
  my_real_var [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1.41[/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'my_integer_var = '[/color], my_integer_var
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'my_real_var    = '[/color], my_real_var
[COLOR=#a020f0]end program[/color] include_example
Now I use the g95 compiler option -E to see what happens after preprocessing
Code:
$ g95 include_example.f90 -E                
program include_example
  !" include source with variable declarations
integer :: my_integer_var
real :: my_real_var

  ! use the variables declared in the include file
  my_integer_var = 5
  my_real_var = 1.41

  write(*,*)'my_integer_var = ', my_integer_var
  write(*,*)'my_real_var    = ', my_real_var
end program include_example
You see, that the source file with variables declaration was included in the main program source file.
Now I can compile and run the program:
Code:
$ g95 include_example.f90 -o include_example

$ include_example
 my_integer_var =  5
 my_real_var    =  1.41

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top