Re: reading configuration files



Bart Vandewoestyne wrote:

Hello all,

In an attempt to make a certain program more flexible in its use,
I would like it to read certain parameters from a textual
configuration file.

For as far as I can see now, the program parameters are all
simple scalar values, so I was wondering what could be the best
format for my configuration file, with respect to readability,
ease of reading in the values in my program and also taking into
account that the people who are entering the values in the
text-files do not know much about Fortran programming.

Some ways of doing it could be for example:

Option 1: use one space
----------------
parameter1 value1
parameter2 value2

Option 2: use :
---------------
parameter1:value1
parameter2:value2

Option 3: use tabs
------------------
parameter1 value1
parameter2 value2

Option 4: use two lines for parameter-value pairs
-------------------------------------------------
parameter1
value1
parameter2
value2

Option 5-...: any other suggestions?
------------------------------------
???

The names 'parameter1' and 'parameter2' do not necessarily have
to be the same length, and also I do not want to make many
assumptions on the input format of the numerical values (say I have
non-fortran users that need to modify the text-files and write
numerical values in it... so they don't know anything about
format descriptors etc...)


What would be a good format for my configuration files so that
i can easily read it in my fortran code and so that it is a bit
robust against different formats for the numerical values.
(e.g. a user could enter 1.0e3 or 1000 or 1000.0.

Thanks,
Bart


Since we are talking Fortran here,
consider to use NAMELIST input.

Just an example:

Input file:
&NML1
I = 0,
J = 0,
K = 0,
A = 0.0000000E+00,
B = 0.0000000E+00,
C = 0.0000000E+00,
D = 0.0000000E+00,
STRING = "a string like this"
/
&NML2
L = F,
M = 0,
N = 0,
STRING2 = "here is the end"
/

Can simply read by this program:

program nmltest

integer i,j,k,m,n
logical l
real a,b,c,d
character*16 string, string2

namelist /nml1/ i,j,k,a,b,c,d,string
namelist /nml2/ l,m,n,string2

read(*,nml1)
read(*,nml2)

end




--

--
Joseph Huber, http://www.huber-joseph.de
.



Relevant Pages

  • Re: Variable length/precision formats?
    ... "&" is contunuation in Fortran anyway. ... purpose of string substitution. ... Something more general purpose, although for general purpose use, I'd prefer it also support substitution of character strings (, not that it makes any sense for the numerical portion of a format, but you could use it to change from say I format to Z format (and in a wide variety of other places as well). ...
    (comp.lang.fortran)
  • Re: How to write a 2d array to a file
    ... matrix in free format. ... This is fixed form source for G77. ... format string is ') ... 2D Fortran arrays are stored by column. ...
    (comp.lang.fortran)
  • Re: fortran equivalent of matlab num2str
    ... Or What isthe corresponding fortran statement for Matlab num2str? ... default '*' format. ... character:: string ... 'before and after * to show extent of whitespace) is:' ...
    (comp.lang.fortran)
  • Re: fortran equivalent of matlab num2str
    ... Or What isthe corresponding fortran statement for Matlab num2str? ... default '*' format. ... character:: string ... 'before and after * to show extent of whitespace) is:' ...
    (comp.lang.fortran)
  • Re: Infinite Loops and Explicit Exits
    ... > One of the features of COBOL which I like is that the format has to ... reference notation or STRING. ... based system and a user selectable list to specify the output fields, ...
    (comp.lang.cobol)

Loading