Register to reply

Converting integer data to string in fortran 90

by s_hy
Tags: converting, data, fortran, integer, string
Share this thread:
s_hy
#1
Mar13-12, 12:22 AM
P: 56
hi all,

i have the following code:

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.

thanks
Phys.Org News Partner Science news on Phys.org
NASA team lays plans to observe new worlds
IHEP in China has ambitions for Higgs factory
Spinach could lead to alternative energy more powerful than Popeye
AlephZero
#2
Mar13-12, 11:28 AM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,924
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.
gsal
#3
Mar13-12, 07:57 PM
P: 872
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 :-)

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.

s_hy
#4
Mar13-12, 09:49 PM
P: 56
Converting integer data to string in fortran 90

thank you gsal....it's work...


Register to reply

Related Discussions
Converting integer into array of single digits in C#? Programming & Computer Science 4
Converting VBA to Fortran Programming & Computer Science 2
Converting string to integer using atoi in C++ Programming & Computer Science 6
Converting equations to Fortran code Programming & Computer Science 1
C code, converting float into integer Programming & Computer Science 16