Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Dinamic format specifier

Status
Not open for further replies.

Feddozz

Mechanical
Dec 15, 2005
16
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
Code:
FORMAT(N(F10.2))
where N is stored in a real variable and changes.
Can you help me?
Tnx
 
Replies continue below

Recommended for you

Is it possible to read them in unformatted? For instance,

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.

 
use a character string for your format specifier

character*10 String
string='( F10.2)'
.
.
.
write(string(2:4),'(I3)')N
.
.
read(*,string)(a(i),i=1,N)
.
.


 
Hi,
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:
write(string(2:4),'(I3)')N
I've never seen it.

Tnx
 
If it is possible to convert the content of an integer variable to a string the dynamic format could work as follows:
Code:
 PROGRAM DYNFOR
  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:
CHAR()
but I realised that it converts the integer in the correspondent ASCII char

Does someone knows if this type of conversion is possible in FORTRAN?
 
Feddozz

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.

 
Thank you very much, johnhors
It works absolutely fine!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor