Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran Issue

Status
Not open for further replies.

coil87

Programmer
Jul 16, 2013
2
Hi everyone!
I have a problem in compiling a program i made in fortran because of an error that occur in this line:
str=char(iachar('0')+ord/10)char(iachar('0')+mod(ord,10))'.par'
open(lin,file=str,status='old')
I have defined ord as an integer, and str as a character, and that particular portion of code is insered in a do cicle (do ord=1,10) because i want that the program open the file named 01.par 02.par ... 09.par 10.par

How can i fix it?
Thank you for your answers
 
For converting number to string use write and for concatenation of strings use //

Example:
Code:
[COLOR=#a020f0]program[/color] num_str
  [COLOR=#2e8b57][b]integer[/b][/color] :: nr
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]2[/color] :: nr_char
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]12[/color] :: fname

  [COLOR=#804040][b]do[/b][/color] nr [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]10[/color]
    [COLOR=#804040][b]write[/b][/color](nr_char,[COLOR=#ff00ff]'(i2.2)'[/color]) nr
    fname [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'file_'[/color][COLOR=#804040][b]//[/b][/color]nr_char[COLOR=#804040][b]//[/b][/color][COLOR=#ff00ff]'.par'[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] num_str

Output:
Code:
$ gfortran num_str.f95 -o num_str

$ num_str
 file_01.par 
 file_02.par 
 file_03.par 
 file_04.par 
 file_05.par 
 file_06.par 
 file_07.par 
 file_08.par 
 file_09.par 
 file_10.par
 
Code:
str=char(iachar('0')+ord/10)char(iachar('0')+mod(ord,10))'.par'

This instruction is wrong. You need to merge string using the operator // :

Code:
str=char(iachar('0')+ord/10)//char(iachar('0')+mod(ord,10))//'.par'

But as mentioned by mikrom, another technique is possible : the internal write. For instance :

Code:
write(str,"(I0,A)") ord,'.par'

Notice that the format I0 (instead of I2.2 as proposed by mikrom) is more general and valid for any integer number.



François Jacq
 
But using this format descriptor
Code:
write(nr_char,[highlight]'(I2.2)'[/highlight]) nr
you will get leading zeros in filenames
Code:
file_[highlight]01[/highlight].par
...

Using this format descriptor
Code:
write(nr_char,[highlight]'(I0)'[/highlight]) nr
suppresses leading zeros, so you will get
Code:
file_[highlight]1[/highlight] .par 
...
 
Thank you so much for your answers! I appreciate your collaboration!

If i want to insert the line
Code:
str=char(iachar('0')+ord/10)//char(iachar('0')+mod(ord,10))//'.par'
or
Code:
write(nr_char,'(I2.2)') nr
in a Subroutine such as
Code:
program main
 do ord=1,10
call read
end do
end main
subroutine read
 write(nr_char,'(I2.2)') ord
 fname = 'file_'//nr_char//'.par'
 open(1,file=fname,status'old')
How can i fix this problem?
That's the real case i'm interested in to.
Thank you for your patience, i've recently started programming in fortran
 
You have to get ord or fname as an argument of the subroutine, i.e
Code:
subroutine read(ord)
  integer, intent(in) :: ord
  ...
end subroutine read
or
Code:
subroutine read(fname)
  character(*), intent(in) :: fname
  ...
end subroutine read
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top