×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

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!

*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

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

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. 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