[英]Fortran trim or adjustl not working while using twice
I am trying to use trim
/ adjustl
for the following code. 我正在尝试对以下代码使用
trim
/ adjustl
。 It seems that I'm getting either X_eq_ 10.0.dat
or X_eq_10.0 .dat
as the output file's name where I'm expecting it to be X_eq_10.0.dat
(no blank space). 看来我正在将
X_eq_ 10.0.dat
或X_eq_10.0 .dat
作为输出文件的名称,我希望它是X_eq_10.0.dat
(无空格)。 Any remedy? 有补救办法吗?
Program Test
double precision:: X
character (len=10) :: tag
character (len=100) :: outfile
X=10.0
write(tag,'(f10.1)') X
print*,'tag=',tag
outfile='X_eq_'//trim(tag)//'.dat'
print*,'Output file: ',outfile
outfile='X_eq_'//trim(tag)//trim('.dat')
print*,'Output file: ',outfile
outfile='X_eq_'//adjustl(trim(tag))//adjustl(trim('.dat'))
print*,'Output file: ',outfile
End Program Test
I have used gfortran
as the compiler. 我已经使用
gfortran
作为编译器。
What you want is: 您想要的是:
outfile='X_eq_'//trim(adjustl(tag))//'.dat'
adjustl
shifts the characters left, leaving trailing blanks, so you need to trim that result. adjustl
将字符向左移动,尾随空白,因此您需要修剪该结果。 It does no good to do trim(tag)
as that is already right-adjusted. 做
trim(tag)
并没有好处,因为已经进行了正确的调整。 Lastly, '.dat'
doesn't need any processing. 最后,
'.dat'
不需要任何处理。
In 在
write(tag,'(f10.1)') X
we say that we want tag
to be of width 10 with one digit in the fractional part. 我们说我们希望
tag
的宽度为10,小数部分为一位。 With the one decimal symbol that leaves us 8 places before the decimal: there will be blank padding beyond the (optional) sign. 由于有一个小数点符号,使我们在小数点前保留了8位:除了(可选)符号之外,还有空白填充。
This is why we see lots of blanks in outfile='X_eq_'//trim(tag)//'.dat'
. 这就是为什么我们在
outfile='X_eq_'//trim(tag)//'.dat'
看到很多空白的原因。
We can avoid this either with adjustl
as noted in the question or another answer, or by using 0
in the edit descriptor: 我们可以通过问题或其他答案中提到的
adjustl
或在编辑描述符中使用0
来避免这种情况:
write(tag,'(F0.1)') X
The F0.d
form makes the field width the smallest appropriate field with: without leading blanks. F0.d
格式使字段宽度成为最小的合适字段,并带有:无前导空格。
When tag
has length 100 there will still be (lots of) trailing blanks, so a trim
will be necessary. 当
tag
长度为100时,仍然会有(很多)尾随空白,因此必须进行trim
。
Further, there are even ways to avoid using an intermediary such as tag
without using trim
. 此外,甚至有一些方法可以避免使用
tag
的中介而不使用trim
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.