Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange fortran behavior: string manipulation

Status
Not open for further replies.

JoggerRunner

Technical User
Jun 13, 2016
3
DE
Hi ut there,

using the code of Link I want to add strings to an input string, requested by the program. The new strings should be the name of existing data files. But one of them has an extra character at the last position. I can not find out what happens here. Here is a minimal code:


Code:
       program test
c
       character*8 filin
       character*12 dummy,file1,file2
       character*4  :: img = '.IMG', doc='.DOC'
       integer*4 ls1, ls2, i
c
       write(*,*) ' File (ohne Extension): '
       read(*,'(a8)') filin
       
       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      zweiter 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


For example, if you type in Ach_40, the output is Ach_40.DOC? and Ach_40.IMG. So, where does the question mark come from?

Thanks a lot
the jogging runner
 
Try using read(*,*) filin


Bill
Lead Application Developer
New York State, USA
 
Hi Bill,

thanks a lot for your answer. The error still appears:

Bildschirmfoto_2016-06-13_um_17.05.34_uyytur.png
 
I tried your example and it seems to work
Code:
mikrom@mikrom-desktop ~/Documents $ gfortran JoggerRunner.f95 -o JoggerRunner
mikrom@mikrom-desktop ~/Documents $ ./JoggerRunner
  File (ohne Extension): 
ACH_40
 ACH_40.DOC
 ACH_40.IMG
 
Well, it may work here or it may not; but there are a couple of things I would recommend.

1.- Do not use formatted READ, stick to list oriented; instead of READ(*,'(A8)'), simply do READ(*,*)
2.- Trim strings before using/concatenating them; instead of dummy=filin//doc, do dummy=trim(filin)//trim(doc)

 
The code can be made much simpler. Use the following. It also handles not having the input starting on the next line

Code:
        program test
c
       character*8 filin
       character*12 file1,file2
       character*4  :: img = '.IMG', doc='.DOC'
       integer*4 ls1, ls2, i
c
       write(*,101,advance='no')
101    format(' File (ohne Extension): ',a)
       read(*,*) filin
       
       file1=trim(filin)//trim(doc)
             
c      zweiter File

       file2=trim(filin)//trim(img)
       write(*,*) file1
       write(*,*) file2
       stop
       end

Bill
Lead Application Developer
New York State, USA
 
@Beilstwh:
1. ok, but if I'm right trim only removes trailing spaces. Then I would suggest to adjust string to left before using trim, like:
Code:
file1=trim(adjustl(filin)) // doc
...etc.
2. Maybe OP wants to remove the spaces inside of the string too, e.g. he gets on the input " ACH_ 40" and wants to make from this "ACH_40", therefore he uses the do-loop. However good practice would be to use a subroutine or function and don't repeat the same code twice.

@JoggerRunner: btw, instead of spaces you can get on the input TABs too, then your procedure will not work properly.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top