Extraneous output when using an unlimited format item

旧时模样 提交于 2021-02-05 07:57:05

问题


This is my code:

Program Output_Format

Implicit none

Integer::k
Integer,parameter:: Br_nn_mre = 5
Character(56),parameter:: FMT_01 = '(1x,"NN_DM:",1x,*("NN_",i2.2,1x))'

Open( 15 , File = 'Output.txt' , Status = 'Unknown' , Action = 'Write' )

  Write( 15 , FMT_01 ) ( k , k = 1 , Br_nn_mre )

Close( 15 , Status = 'Keep' )

End Program Output_Format

The content of Output.txt file is:

NN_DM: NN_01 NN_02 NN_03 NN_04 NN_05 NN_

I want to get this content in Output.txt:

NN_DM: NN_01 NN_02 NN_03 NN_04 NN_05

That is, without the trailing NN_

What is wrong with * IN FMT_01 format? For example if I put a 5 in place of * I will get what I want. How can I use the unlimited repeat count and still get the desired output? I won't always know how many times to repeat.


回答1:


This is related to how formats are processed, and in particular, when a data transfer statement terminates.

For an output statement such as you have, transfer terminates when either:

  • a data edit descriptor is reached and there is no remaining element in the output list; or
  • the final closing parenthesis is reached and there is no remaining element in the output list.

In your formats

'(1x,"NN_DM:",1x,*("NN_",i2.2,1x))'

and

'(1x,"NN_DM:",1x,5("NN_",i2.2,1x))'

the single data edit descriptor there is the i2.2. The 1xs are control edit descriptors and the "NN_DM" and "NN_" are character string edit descriptors.

Let's look at how your format is processed in the case of 5 as the repeat count. The first part of the format 1x,"NN_DM:",1x is processed without issue giving output NN_DM: moving us on to 5("NN_",i2.2,1x)). Corresponding to this repeated fragment are five data items, so they are processed (giving output NN_01 NN_02 NN_03 NN_04 NN_5).

The important part is what happens next. After completing this 5(..) part we reach the final closing parenthesis of the format specification and there is no remaining output item, so processing of the format comes to an end.

What's different with the *(..) case?

Well, when we reach the end of *(..) we go back round to the start of that repeated format; we don't move on to the final closing parenthesis.1 That leaves us to process the edit descriptors until we reach a data edit descriptor. This means that "NN_" is processed (resulting in NN_ being output) before we notice that we are out of data items for output.

Coming to the fix: use the colon edit descriptor. The colon edit descriptor acts like a data edit descriptor in the sense that format processing terminates immediately if there is no remaining data item.

Character(56),parameter:: FMT_01 = '(1x,"NN_DM:",1x,*("NN_",i2.2,:,1x))'

Personally, I would write this as

Character(*),parameter:: FMT_01 = '(" NN_DM:",*(" NN_",i2.2,:))'

1 This would be no different if we had 6 as the repeat count; * isn't special except that it is a "very large repeat count".



来源:https://stackoverflow.com/questions/48052837/extraneous-output-when-using-an-unlimited-format-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!