Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
28 Views

Error converting character variable

Jump to solution

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

Accepted Solutions
Highlighted
New Contributor III
28 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

0 Kudos
9 Replies
Highlighted
Valued Contributor II
28 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 ;).

0 Kudos
Highlighted
28 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        

 

 

 

0 Kudos
Highlighted
Valued Contributor II
28 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.

 

0 Kudos
Highlighted
New Contributor III
29 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

0 Kudos
Highlighted
28 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.

0 Kudos
Highlighted
28 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
0 Kudos
Highlighted
Black Belt Retired Employee
28 Views

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

0 Kudos
Highlighted
28 Views

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

 

 

 

0 Kudos
Highlighted
New Contributor III
28 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.

0 Kudos