View Full Version : converting an integer to character
hydraulic_M.H.
06-15-2004, 01:39 PM
Hi,
I 'd like to convert an INTEGER to a CHARACTER in fortran 90.
Of course I don't want to write the number to a scratch file and read from it.
Because the number of digits in integer variable is not predefined and varies in each iteration in my program.
Please help me!
Bye
M.H.
tzeis
06-15-2004, 05:46 PM
You don't want to use a scratch file, you want to use an internal file. The code might look like this:
integer :: i
character(len=10) :: ci
!...
write(ci,*) i
Everything stays in memory, so it is much faster than writing a scratch file.
hydraulic_M.H.
06-17-2004, 09:17 AM
Hi,
I shall apprecaite you very much for helping me, It now works, but there is still another problem.
My aim is to produce automatically a file at a time in the loop with the name ending in a counter, for example:
in first loop: h1.txt
2nd loop : h2.txt
.
.
.
100th loop : h100.txt
and I want to do it as follw:
filename = 'h' // i // '.txt'
the problem here is that ' i ' is an integer that is converted to character with length = 3,
but ' i ' may range from 1 to 100 , it means from one digit to three digit, therefore if I specify length = 3 , and the integer value is 1 , then two blanks will be left in the name of the file like this : 'h1 .txt' and it can not be read by other part of my progrom.
I hope I would have been able enough to describe my problem.
waiting forward to hear from you
M.H.
tzeis
06-17-2004, 06:08 PM
Fortran 90 contains the intrinsic functions that will solve your problem. The functions of interest are ADJUSTL and TRIM. Once you have done the conversion from integer to character, use the ADJUSTL function to left-justify the string:
i = adjustl(i)
and then TRIM the string as you are concatenating it:
filename = 'h' // trim(i) // '.txt'
hydraulic_M.H.
06-22-2004, 10:53 AM
Hi,
I would like just to drop a line to appreciate you for taking time to help me so much.
Wish you all the best
vBulletin® v3.6.8, Copyright ©2000-2012, Jelsoft Enterprises Ltd.