×
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

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 - Integrating the Engineering Ecosystem
Aras Innovator provides multiple options for integrating data between systems, depending on the scenario. Utilizing the right approach to meet specific business requirements is vital. These needs range from authoring tools, federating data from various and dissimilar databases, and triggering processes and workflows. Download Now
Research Report - Simulation-Driven Design for SOLIDWORKS Users
In this engineering.com research report, we discuss the rising role of simulation and the paradigm shift commonly called the democratization of simulation. In particular, we focus on how SOLIDWORKS users can take advantage of simulation-driven design through two analysis tools: SOLIDWORKS Simulation and 3DEXPERIENCE WORKS. Download Now
White Paper - Industry 4.0 and the Future of Engineering Education
With industries becoming more automated, more tech-driven and more complex, engineers need to keep their skills and knowledge up to date in order to stay on top of this wave—and to be prepared for the Industry 4.0 future. The University of Cincinnati offers two online Master of Engineering degree programs designed specifically for practicing engineers. 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