×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Dinamic format specifier

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

CODE

FORMAT(N(F10.2))
where N is stored in a real variable and changes.
Can you help me?
Tnx

RE: Dinamic format specifier

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.

RE: Dinamic format specifier

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)
.
.


RE: Dinamic format specifier

(OP)
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

RE: Dinamic format specifier

(OP)
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?

RE: Dinamic format specifier

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.

RE: Dinamic format specifier

(OP)
Thank you very much, johnhors
It works absolutely fine!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

eBook - Manufacturing the Cars of Tomorrow
In this ebook, we'll explore how additive manufacturing is going to transform the way cars are made. This includes commentary from thought leaders such as Ford's CTO, Ken Washington, Customer case studies of ways 3D printing is being used today, and a variety of part examples where 3D printing is already impacting how automobiles are made. Download Now
White Paper - Smart Manufacturing for Semiconductor
New technologies and approaches present great opportunities for semiconductor manufacturers to achieve high levels of innovation, yield and improvement. This white paper explores some of these cutting-edge technologies and how they can be applied effectively in the semiconductor industry. Read about how Smart Manufacturing is transforming the semiconductor industry. Download Now
White Paper - Analysis and Simulation in Aircraft Structure Certification
Organizations using simulation and analysis tools effectively see the benefits in their ability to achieve certification faster and with drastically less total cost than those who do not maximize these tools. Read this White Paper to learn about how digital tools such as analysis and simulation help in aircraft structure certification. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close