Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Converting integer data to string in fortran 90

  1. Mar 13, 2012 #1
    hi all,

    i have the following code:

    Code (Text):

    do n = ninit,nlast  
    character(len=20) :: filename
    integer :: n
    do n = 1,600
      write (filename, "I0") n
      open (unit=110,file='wave'//trim(filename)//'.dat',action = 'write',status = 'old')
      do i = iinit+1,ilast-1
       !boundary condition
         u(iinit,n+1) = 0
         u(ilast,n+1) = 0
         !end of boundary condition
         u(i,n+1) = 2*(1-(alpha**2))*u(i,n)-u(i,n-1) + (alpha**2)*(u(i+1,n)+u(i-1,n))

       Print*,'i,n,u(i,n+1)=',i,' ', n,' ', u(i,n+1)  
       Write (110,*)'i,n,u(i,n+1)=',i,' ', n,' ', u(i,n+1)
       close  (unit = 110)
       end do !i
    end do !n

    what actually i want is to produce automatically a file at a time in the loop with the name ending in a counter, for example: wave1.dat, wave2.dat,wave3.dat,.......,wave599.dat,wave600.dat...but, my programming didn't run properly. Can anyone advice what is wrong with my code.

  2. jcsd
  3. Mar 13, 2012 #2


    User Avatar
    Science Advisor
    Homework Helper

    If you say status='old', the file you want to open must already exist. Therefore your program problably won't work the first time your run it. (But it would work the second time, except that it didn't work the first time ... catch 22!)

    You don't really need action='write' either, but it won't do any harm.
  4. Mar 13, 2012 #3
    I personally would pad the numbers with zeroes on the left so that when you do a directory listing they actually show up in order :-)

    Code (Text):

    program zzz
    character(len=20) :: filename
    integer :: n
    do n = 1,100
      write (filename, "('wave',I3.3,'.dat')") n
      open (unit=110,file=filename,status = 'new')
      do i = iinit+1,ilast-1
       write (110,*)' whatever '
       close  (unit = 110)
       end do !i
      end do !n
    end program zzz
    this works.
  5. Mar 13, 2012 #4
    thank you gsal....it's work...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook