INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

strange fortran behavior: string manipulation

strange fortran behavior: string manipulation

(OP)
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 --> Fortran

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

RE: strange fortran behavior: string manipulation

Try using read(*,*) filin

Bill
Lead Application Developer
New York State, USA

RE: strange fortran behavior: string manipulation

(OP)
Hi Bill,

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

RE: strange fortran behavior: string manipulation

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 

RE: strange fortran behavior: string manipulation

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)

RE: strange fortran behavior: string manipulation

(OP)
Thanks a lot for your hints!! Now, it works

RE: strange fortran behavior: string manipulation

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

RE: strange fortran behavior: string manipulation

@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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close