1

In Fortran90 I would like to set the format of an output using a variable. My code looks like this:

fileUnit = 14
myFormat = '(10e18.10)' 
write (fileUnit,myFormat) myData

The value of myFormat can be any allowed for the type of myData. Is there a way to set myFormat such that the output is equivalent to coding

write (fileUnit,*) myData
4
  • 2
    You want a format equivalent to the *? There isn't any. But perhaps I don't understand your question. What does the sentence The value of myFormat can be any allowed for the type of myData. mean? Commented Oct 30, 2015 at 14:59
  • I'm sure you know but it should be myFormat = '(10e18.10)' Commented Oct 30, 2015 at 19:55
  • @VladimirF: Yes, I am basically looking for a format equivalent to the *. The sentence you highlighted just means that I'd like myData to be of any data type for which a standard output is defined. Commented Nov 2, 2015 at 13:55
  • @agentp: sure, thanks for spotting it! Commented Nov 2, 2015 at 13:56

2 Answers 2

1

You cannot use a bare * in an explicit format specification like you can in a read or write statement. In the explicit format context, * represents an unlimited-format-item group that gives an infinite repeat count to a group of format-items rather than representing list directed formatting (see Cl. 9.6.2.2, R915, Fortran 2008).

The character variable holding your explicit format can be constructed at run-time, so your option to handle dynamic needs is to write code to determine the proper format specification based upon your data. Depending on your compiler, and if your data is a derived type, you may also have the option of defined output (See Cl. 9.6.4.8.3 Fortran 2008) to handle your needs.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to programmatically switch between explicit and list directed format you could do something like this:

 if(myFormat.eq.'*')then
      write(unit,*)...
 else
      write(unit,myFormat)...
 end if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.