Dinamic format specifier
Dinamic format specifier
(OP)
Hi,
I need to read N values in the same row of a file.
The number of values changes so I need to have a dinamic format specifier.
For example
where N is stored in a real variable and changes.
Can you help me?
Tnx
I need to read N values in the same row of a file.
The number of values changes so I need to have a dinamic format specifier.
For example
CODE
FORMAT(N(F10.2))
Can you help me?
Tnx
RE: Dinamic format specifier
N=10
read(40,*) (A(I),I=1,N)
...
blah blah blah, more code...now
N=12
read(40,*) (A(I),I=1,N)
etc..
the first time you read file 40, you'll read a row of 10 numbers. the second time, you'll read 12 numbers in one row.
RE: Dinamic format specifier
character*10 String
string='( F10.2)'
.
.
.
write(string(2:4),'(I3)')N
.
.
read(*,string)(a(i),i=1,N)
.
.
RE: Dinamic format specifier
Thanks for the replies.
@prost
As I told you in the other post your suggestion is correct, with it I understood that my problem is different.
After reading I need to write these values and in that case I really need a format specifier, because if I use an unformatted write, after a certain number of written values (about 5 values) the PC decides to continue in another row. Instead I need to write the whole array in a row even if it is long 15 values.
@johnhors
Can you please explain me the meaning of the following line,
CODE
Tnx
RE: Dinamic format specifier
CODE
CHARACTER A*4
N=12
A= !A STATEMENT THAT CONVERTS THE CONTENT OF N IN '12'
1 FORMAT(A(F10.3))
END
I tried to use the casting statement
CODE
Does someone knows if this type of conversion is possible in FORTRAN?
RE: Dinamic format specifier
Firstly, instead of using a format statement, the code I showed you above used a character string to describe the desired format. The advantage of this is that the character string can be modified at run time to suit your requirements, whilst under standard fortran rules a format statement cannot be modified (although some NON-standard extensions to the standard with some compilers have allowed this happen in the past).
So if N equals 5 then :-
string='( F10.2)'
write(string(2:4),'(I3)')N
would make string = '( 5F10.2)' ,then
read(*,string)(a(i),i=1,N)
is equivalent to:-
read(*,10)(a(i),i=1,N)
10 FORMAT(5F10.2)
All of which answers your original posting.
RE: Dinamic format specifier
It works absolutely fine!!