Converting integer data to string in fortran 90

Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 programming issue related to converting integer data to string format for file naming. Participants explore how to generate filenames dynamically within a loop to create multiple output files, addressing specific coding errors and suggestions for improvement.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their code and the intended output, seeking advice on why it does not function as expected.
  • Another participant points out that using 'status='old'' requires the file to already exist, which may cause the program to fail on the first run.
  • A different approach is suggested, where filenames are padded with zeroes to ensure proper ordering in directory listings.
  • A sample program is provided that successfully implements the suggested filename formatting and file creation process.
  • One participant confirms that the provided solution works for their needs.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to file status and naming conventions, but there are differing opinions on the necessity of certain parameters and the best approach to file naming.

Contextual Notes

Some limitations include assumptions about the existence of files and the specific formatting of filenames, which may affect the program's execution depending on the context of use.

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...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · 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