FORTRAN 中的字符串操作:奇怪的行为
string manipulation in FORTRAN: strange behavior
如果以下工作示例的输入是 - 例如 - "ach_40",则输出为 "ach_40.DOC?" 和 "ach_40.IMG"。哪儿是 ”?”来自?
密码是:
program test
character*8 filin
character*12 dummy,file1,file2
character*4 :: img = '.IMG', doc='.DOC'
integer*4 ls1, ls2, i
write(*,*) ' File (without extension): '
read(*,'(a8)') filin
c first file
dummy=filin // doc
ls1 = len_trim(dummy)
ls2=0
do i = 1,ls1
if(dummy(i:i).ne.' ') then
ls2=ls2+1
file1(ls2:ls2) = dummy(i:i)
endif
enddo
c second file
dummy=filin // img
ls1 = len_trim(dummy)
ls2=0
do i = 1,ls1
if(dummy(i:i).ne.' ') then
ls2=ls2+1
file2(ls2:ls2) = dummy(i:i)
endif
enddo
write(*,*) file1
write(*,*) file2
stop
end
非常感谢您的提示!!
你永远不会设置整个 file1
和 file2
的值,所以你没有明确设置的字符可以是任何东西。
一开始可以设置初始化字符串为
file1 = ''
file2 = ''
它们将填满您需要的空格。
但您可能只想:
file1 = trim(filin) // doc
file2 = trim(filin) // img
而不是所有那些复杂的代码。
如果以下工作示例的输入是 - 例如 - "ach_40",则输出为 "ach_40.DOC?" 和 "ach_40.IMG"。哪儿是 ”?”来自?
密码是:
program test
character*8 filin
character*12 dummy,file1,file2
character*4 :: img = '.IMG', doc='.DOC'
integer*4 ls1, ls2, i
write(*,*) ' File (without extension): '
read(*,'(a8)') filin
c first file
dummy=filin // doc
ls1 = len_trim(dummy)
ls2=0
do i = 1,ls1
if(dummy(i:i).ne.' ') then
ls2=ls2+1
file1(ls2:ls2) = dummy(i:i)
endif
enddo
c second file
dummy=filin // img
ls1 = len_trim(dummy)
ls2=0
do i = 1,ls1
if(dummy(i:i).ne.' ') then
ls2=ls2+1
file2(ls2:ls2) = dummy(i:i)
endif
enddo
write(*,*) file1
write(*,*) file2
stop
end
非常感谢您的提示!!
你永远不会设置整个 file1
和 file2
的值,所以你没有明确设置的字符可以是任何东西。
一开始可以设置初始化字符串为
file1 = ''
file2 = ''
它们将填满您需要的空格。
但您可能只想:
file1 = trim(filin) // doc
file2 = trim(filin) // img
而不是所有那些复杂的代码。