Fortran Converting integer data to string in fortran 90

Click For Summary
The discussion focuses on converting integer data to string in Fortran 90 to create uniquely named files in a loop. The original code attempted to open files with a status of 'old', which required the files to already exist, causing issues on the first run. A suggested solution involved changing the file status to 'new' and formatting the filename to include zero-padding for better sorting. The corrected code successfully generates files named wave001.dat through wave100.dat. The user confirmed that the revised approach worked effectively.
s_hy
Messages
57
Reaction score
0
hi all,

i have the following code:

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
 
Technology news on Phys.org
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.
 
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:
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.
 
thank you gsal...it's work...
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K