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!

char intrinsic

Status
Not open for further replies.

milenko76

Technical User
Mar 26, 2010
100
PT
I am trying this:
id1=iter/10
id2=iter-id1*10
tfile(3:4)=char(id1+48)//char(id2+48)
I have defined:
data tfile/'nr .res'/
Still i get file like this:
nr .res
I would like to be nr01.res,... and so on.
Where could be a problem?
 
I don't see any problem even if I would have choose another solution :

Code:
program test
  implicit none
  character(20) :: tfile="nr  .res"
  integer :: iter,id1,id2
  iter=22
  id1=iter/10
  id2=iter-10*id1
  tfile(3:4)=CHAR(id1+48)//CHAR(id2+48)
  write(*,*) tfile
end program

With the result :

Code:
[lcoul@localhost test]$ ifort t45.f90
[lcoul@localhost test]$ ./a.out
 nr22.res

Here is the solution I propose, which provides the same result :

Code:
program test
  implicit none
  character(20) :: tfile
  integer :: iter
  iter=22
  write(tfile,"(A,I0,A)") "nr",iter,".res"
  write(*,*) tfile
end program

This solution is in the same time shorter and more general because it works with any value for iter.

Notice that it is sometimes still more efficient to use the format "I8.8" (or I9.9) rather than "I0". Indeed, with "I8.8", all the file names keep the same length and, more important, are created in respecting the alphabetic order !

Example :

Code:
program test
  implicit none
  character(20) :: tfile
  integer :: iter
  iter=22
  write(tfile,"(A,I9.9,A)") "nr",iter,".res"
  write(*,*) tfile
  iter=155
  write(tfile,"(A,I9.9,A)") "nr",iter,".res"
  write(*,*) tfile
end program

With the result :

Code:
[lcoul@localhost test]$ ifort t45.f90
[lcoul@localhost test]$ ./a.out
 nr000000022.res     
 nr000000155.res


François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top