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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange behavior with string passing in fortran 77 (gfortran,g77,f77)

Status
Not open for further replies.

aschaeffer

Programmer
May 12, 2010
2
IE
Hi,

I've attached a small program extracted from a large program I'm trying to compile in Linux 64 (Ubuntu 9.10). Basically it is a wrapper function to open a file where a directory prefix is automatically added. This is legacy code from an old project (with thousands of lines). When a quoted string is passed to the subroutine, additional text is added to the variable. However, when passed as a string in a variable, this does not occur. Is passing a quoted string in this manner (Test 1 below) not a legal manner in fortran 77? The program, the compiler instruction, and the output I get is in the comments at the end. Thanks for any help, Andrew.
------------------------------------------------------------
C Output from Public domain Ratfor, version 1.01
program ami3d
character*100 testfile
print *,' Test 1:'
print *, ' Testing openfile for quoted string'
print *,kar('testfile123')
call openfile(12,'testfile123')

print *,' '
print *,' Test 2:'
print *,' Testing openfile for passed variable'
testfile="testfile123"
print *,kar(testfile)
call openfile(12,testfile)
end


function kar(string)
character*100 string
kk=100
ii=ichar(string(kk:kk))
800 if((ii.gt.126 .or. ii.lt.33) .and. kk.gt.1)then
kk=kk-1
ii=ichar(string(kk:kk))
goto 800
endif
801 continue
kar=kk
return
end


subroutine openfile(nfile,fname)
! common /opname/ fitdir
character*100 fitdir, fname
write(6,'(a,a,a)') '-',fname(1:kar(fname)),'-'
! open(nfile,file=fitdir(1:kar(fitdir))//fname(1:kar(fname)))
return
end


! compile line:
![aschaeff@triton aschaeff]$ gfortran -std=legacy testfilename.f -o amitest
!
! The output from running this program
![aschaeff@triton aschaeff]$ amitest
! Test 1:
! Testing openfile for quoted string
! 74
!-testfile123
! Test 2: Testing openfile for passed variable(a,a,a)--
!
! Test 2:
! Testing openfile for passed variable
! 11
!-testfile123-
![aschaeff@triton aschaeff]$
 
The problem is not in Fortran but in your function kar(). I didn' see the sence why you compute everytime
Code:
ii=ichar(string(100:100))
when your string has less then 100 characters.
I modified the function kar() so, that it first computes the length of the string and this gives you the same result for both cases:
Code:
[COLOR=#0000ff]!  Output from Public domain Ratfor, version 1.01[/color]
      [COLOR=#a020f0]program[/color] ami3d
      [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]100[/color] testfile
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]' Test 1:'[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'  Testing openfile for quoted string'[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],kar([COLOR=#ff00ff]'testfile123'[/color])

      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]' Test 2:'[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'  Testing openfile for passed variable'[/color]
      testfile[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]"testfile123"[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], kar(testfile)
      [COLOR=#a020f0]end program[/color] ami3d


      [COLOR=#a020f0]function[/color] kar(string)
      [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]) string
      kk[COLOR=#804040][b]=[/b][/color][COLOR=#008080]LEN_TRIM[/color](STRING)
      ii[COLOR=#804040][b]=[/b][/color][COLOR=#008080]ichar[/color](string(kk:kk))
[COLOR=#6a5acd]800[/color]   [COLOR=#804040][b]if[/b][/color]((ii[COLOR=#804040][b].gt.[/b][/color][COLOR=#ff00ff]126[/color] [COLOR=#804040][b].or.[/b][/color] ii[COLOR=#804040][b].lt.[/b][/color][COLOR=#ff00ff]33[/color]) [COLOR=#804040][b].and.[/b][/color] kk[COLOR=#804040][b].gt.[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]then[/b][/color]
        kk[COLOR=#804040][b]=[/b][/color]kk[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
        ii[COLOR=#804040][b]=[/b][/color][COLOR=#008080]ichar[/color](string(kk:kk))
        [COLOR=#804040][b]goto[/b][/color] [COLOR=#ff00ff]800[/color]
      [COLOR=#804040][b]endif[/b][/color]
[COLOR=#6a5acd]801[/color]   [COLOR=#804040][b]continue[/b][/color]
      kar[COLOR=#804040][b]=[/b][/color]kk
      [COLOR=#804040][b]return[/b][/color]
      [COLOR=#a020f0]end function[/color] kar
Output:
Code:
$ g95 -g aschaeffer.f95 -o aschaeffer

$ aschaeffer
  Test 1:
   Testing openfile for quoted string
 11
  Test 2:
   Testing openfile for passed variable
 11
 
mikrom, thanks.

That works for me as well. Was the strange output I was getting related to passing a string that was not 100 characters in length to a variable that was expecting a string of 100 characters?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top