2
votes

If the input of the following working example ist - for example - "ach_40", the output is "ach_40.DOC?" and "ach_40.IMG". Where is the "?" coming from?

The code is:

   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 

Thanks a lot for your hint!!

1
Bearing in mind that a) a fixed length string in Fortran always has that same fixed length; and b) if you don't define a variable, it's value is undefined, run through the logic of your program with your example input, and consider "What should I expect the value of the last few characters in file1 to be?". - IanH

1 Answers

3
votes

You never set the value of the whole file1 and file2 so the characters, which you do not explicitly set to something, can be anything.

At the beginning you can set initialize the strings as

file1 = ''
file2 = ''

and they will be filled with spaces which is what you need.


But you probably just want:

file1 = trim(filin) // doc
file2 = trim(filin) // img

instead all of that complicated code.