Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Convert real*8 to character

rostislavhrtus
Beginner
4,521 Views
Hello everybody, I just have a problem with converting, e.g.

Let's have a vector
p= [2.5 2.1 2.5 2.1]

now we have
character*3 par1

And this should work (but its not)
read( par1 , '(i3)' ) p(1)

Problem is, that i really don't know, what does i3 mean.

Is there some better approach for this conversion?

Last thing I have to ask is concatenation (this also is not working):

write(OUT_F,"(a26)")("OUT"//"_text_"//p(1)//"_"//p(2)//"_"
$ //p(3)//"_"//p(4)//"_"//".rep")

I havent found anything what can give mi information what to do. Error is:

error #6358: Constants and expressions are invalid in read-only I/O lists.

And it is not included on Intels website.

Can someone help me, please?

Thank you. Rosa
0 Kudos
4 Replies
mecej4
Honored Contributor III
4,521 Views
Instead of

read( par1 , '(i3)' ) p(1)

you need

write (par1, '(i3)') p(1)

Reason: par1 is an "internal file". Initially, the file is empty. There is nothing there to read; furthermore, you already have a value in p(1). You want to write to the internal file the value of p(1), encoded using the format descriptor i3. This descriptor says that the quantity being encoded is an integer, and that three digits (or blanks and digits) are to be output.

Your write statement is wrong because you are trying to concatenate strings with non-strings.

Read the language manuals, Fortran text-books and use on-line tutorials.
0 Kudos
rostislavhrtus
Beginner
4,521 Views
So the only way to convert real to character is to save real type to a file and then read into character one? I googled this and it seemed to that this conversion should be possible.

Saving p(1) into par1 (character type) was there to substitute it into second term where you correctly mentioned that there is unexpected type. But there's not much information about ways how to do it.
0 Kudos
mecej4
Honored Contributor III
4,521 Views
The phrase "internal file" is potentially confusing to people coming to Fortran programming from a background in other languages. An "internal file" is not a file in the sense used in describing operating systems, file systems, etc. Usually, an internal file is just a block of memory which may be treated as if it is a file (with some restrictions that do not apply to external files) during the lifetime of a program. Compare a Fortran internal file I/O operation to using the C sprintf/sscanf functions.

Here is a small program to illustrate:

[fortran]program explore
real :: p(4) = [2.5, 2.1, 2.5, 2.1]
character(len=3) :: str1

write(str1,'(F3.1)')p(2)
!
! now that p(2) has been encoded into a string, we can concatenate the
! string with other strings
!
write(*,*)' After writing p(2) to str1, str1 is : |' // str1 // '|'
!
end program explore
[/fortran]
0 Kudos
rostislavhrtus
Beginner
4,521 Views
Great, it's working. Thank you a lot!
0 Kudos
Reply