Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Error converting character variable

lobaton_cebrian__edu
196 Views

Hello friends!

I am receiving a warning message of overflow(see attached) when trying to convert character to real.

This is the simple piece of code 

character*4 nLevels
real*4 num_nlevels
 
        write (nlevels, '(I0)') num_nlevels
 
 
Thanks.
Regards.
0 Kudos
1 Solution
Johannes_Rieke
New Contributor III
196 Views

Hm, also not correct. I will encourage you to have a look into the help: https://software.intel.com/en-us/fortran-compiler-18.0-developer-guide-and-reference-format-specific...

What you have done in 1) is to write the real number as 5 digit field to a 4 character long character variable. Does not match, isn't it.Try something like this:

program test
  implicit none
  real(kind=1.0)          ::  num_nlevels
  character(len=100) ::  nLevels

  write(nLevels,'(F5.0)') num_nlevels
  write(*,'(a)') trim(nLevels)

! or
  
  write(nLevels,'(I0)') nint(num_nlevels)
  write(*,'(a)') trim(nLevels)

end program test

ps: Arjen was faster ;-)

View solution in original post

9 Replies
Arjen_Markus
Valued Contributor III
196 Views

Hm, you have defined num_levels as a real and you are using an integer format to read it from the character string. There is a discrepancy here ;).

lobaton_cebrian__edu
196 Views

Thanks Arjen!

My fault, lack of attention, sorry!!!! I made a mistake copying the code.

I tried both; define "num_nLevels"  as integer and real.  In both cases it is giving me back the same error (output statement overflows record)

1)

character*4  nLevels
real *4         num_nlevels
write (nlevels, '(F5.0)' ) num_nlevels        
 
2)
character*4  nLevels
integer*4      num_nlevels
write (nlevels, '(I0)' ) num_nlevels        

 

 

 

Arjen_Markus
Valued Contributor III
196 Views

Well, for case 1 you are trying to put 5 characters into a character string of length 4. And depending on the actual value of num_levels (you do not show it), this sting length might not be enough in case 2 either.

Why not try with a much larger character length? 20 or 40, say. Just to see what happens.

 

Johannes_Rieke
New Contributor III
197 Views

Hm, also not correct. I will encourage you to have a look into the help: https://software.intel.com/en-us/fortran-compiler-18.0-developer-guide-and-reference-format-specific...

What you have done in 1) is to write the real number as 5 digit field to a 4 character long character variable. Does not match, isn't it.Try something like this:

program test
  implicit none
  real(kind=1.0)          ::  num_nlevels
  character(len=100) ::  nLevels

  write(nLevels,'(F5.0)') num_nlevels
  write(*,'(a)') trim(nLevels)

! or
  
  write(nLevels,'(I0)') nint(num_nlevels)
  write(*,'(a)') trim(nLevels)

end program test

ps: Arjen was faster ;-)

View solution in original post

lobaton_cebrian__edu
196 Views

Thank you for your time and your effort. These format problems are annoying issues difficult to handle. Johannes, thank you for the link and the code.

lobaton_cebrian__edu
196 Views

Hello again!

I am sadly stuck in the middle of nowhere and I don't know what else I can try. I am using VisualStudio for Fortran developings. I can not attach a dat file, so I captured screen.

Please someone that tells me why trying  write(strlevels,'(F7.0)') nlevels does not work.

THANK YOU!!

subroutine ExtractData()
      
    integer i
    character*7  strlevels
    real nlevels
    
    open(1,file='C:\file.dat',FORM='binary', ACCESS='sequential')
    read(1)strlevels              
    ! If I comment           write(strlevels,'(F7.0)') nlevels              everything is 0k and C:\file.JSON is successfully created.   
    ! And you can see that STRLEVELS  is 7 characters long
    write(strlevels,'(F7.0)') nlevels  ! --> I think this should be moving STRLEVELS value into NLEVELS, but it fails.
    close(1)  
 
    open(2,file='C:\file.JSON',status='unknown')
    write(2,*)'{"nlevels":"'//strlevels//'"},'
   
 end subroutine ExtractData
Steve_Lionel
Black Belt Retired Employee
196 Views

You want READ and not WRITE if you want to convert the character representation into a REAL.

lobaton_cebrian__edu
196 Views

Once again Doctor, thank you.  Programming in Fortran makes me feel I do not know anything. Thanks again.

 

 

 

Johannes_Rieke
New Contributor III
196 Views

Hi, in case you will like to read and parse *.JSON files

(open(2,file='C:\file.JSON',status='unknown'),

you might like to use a pure Fortran library written by Jacob Williams:

https://github.com/jacobwilliams/json-fortran

To take advantage from this lib, you might need to enhance your Fortran base skills.

Reply