Converting integer data to string in fortran 90

In summary, the code provided involves a loop that creates and opens files with the name "wave" followed by a counter number. However, due to using status="old", the program may not run properly the first time. The suggested solution is to use status="new" and pad the counter numbers for proper ordering.
  • #1
s_hy
61
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
  • #2
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.
 
  • #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:
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.
 
  • #4
thank you gsal...it's work...
 
  • #5


I would suggest that the issue with your code may be related to the use of the write statement. In Fortran 90, the write statement requires a format specifier, which is missing in your code. Without a format specifier, the write statement will not be able to convert the integer data to a string properly.

To fix this issue, you can use the "I0" format specifier in the write statement, which will convert the integer data to a string with the specified number of digits. For example, using "I3" will convert the integer 1 to the string "001".

In addition, I would recommend using the "newunit" keyword in the open statement instead of specifying the unit number manually. This will ensure that the unit number is not already in use and will prevent any potential conflicts.

I hope this helps and good luck with your programming!
 

1. What is the syntax for converting integer data to string in Fortran 90?

The itoa function is used to convert an integer to a string in Fortran 90. The syntax is itoa(value, string), where value is the integer to be converted and string is the resulting string.

2. Can I convert a string to an integer in Fortran 90?

Yes, the atoi function can be used to convert a string to an integer in Fortran 90. The syntax is atoi(string, value), where string is the string to be converted and value is the resulting integer.

3. How do I handle errors when converting integer data to string in Fortran 90?

When using the itoa function, it is important to check for errors. If the conversion fails, the resulting string will be blank. You can check for errors by using the errno function, which will return a non-zero value if an error occurred.

4. Is there a limit to the length of the resulting string when converting integer data to string in Fortran 90?

Yes, there is a limit to the length of the resulting string when using the itoa function. The maximum length is determined by the size of the integer being converted and the size of the string being used to store the result. It is important to ensure that the string is large enough to hold the resulting string.

5. Are there any other functions for converting data types in Fortran 90?

Yes, there are several other functions for converting data types in Fortran 90, such as dtos for converting a double precision number to a string, and stod for converting a string to a double precision number. It is important to use the appropriate function for the data type being converted.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
6K
  • Programming and Computer Science
Replies
1
Views
933
  • Programming and Computer Science
Replies
4
Views
587
Replies
9
Views
1K
Back
Top