Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to read from a string into 8-byte integer?

How to read from a string into 8-byte integer?

How to read from a string into 8-byte integer?

I have the following 8-byte integer defined as follows:

integer*8 :: strINT = 0

And an allocatable string defined using

character(:), allocatable :: str
str = "9876543210"

I'm trying to set the strINT variable using the read function

read(str,*) strINT

However, this gives the following error

Fortran runtime error: Bad integer for item 1 in list input

The read function seems like it does not work with my 8-byte integer. How can I get the strINT variable to be set with the value represented by str?

RE: How to read from a string into 8-byte integer?

Hi MBFerguson,

I tried this:



program mbferguson
  implicit none
  integer*8 :: strint = 0
  character(:), allocatable :: str
  str = "9876543210"
  read(str,*) strint

  write(*, *) "str = ", str
  write(*, *) "strint = ", strint
end program mbferguson 

and it works:


$ gfortran mbferguson.f95 -o mbferguson 
$ ./mbferguson 
 str = 9876543210
 strint =            9876543210 

I have this compiler version:


$ gfortran --version
GNU Fortran (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 

What compiler are you using ?

RE: How to read from a string into 8-byte integer?

Hello mikrom,

I am using the following compiler version:

$ gfortran --version
GNU Fortran (GCC) 11.2.0

I am not necessarily tied down to this specific compiler, although I don't understand why a more recent version would have problems a previous version doesn't have.

Here is my entire source code for the program I'm writing:

program Euler43

implicit none
integer :: time_start, time_finish, clock_finish
integer*8 :: solution = 0

character(:), allocatable :: str
character(3) :: s234, s345, s456, s567, s678, s789, s8910
integer :: i234, i345, i456, i567, i678, i789, i8910
integer*8 :: max_ = 4200000000, strINT = 1402356789
integer, dimension(10) :: reverse, numbers = (/ 1,4,0,2,3,5,6,7,8,9 /)
integer :: i, j, u, x, y, z

call SYSTEM_CLOCK(time_start)

str = "1402356789"
do while(strINT .LT. max_)

do i = 8,0,-1
if(numbers(i+1) .LT. numbers(i+2)) then
x = i
end if
end do

do i = 9,0,-1
if (numbers(x+1) .LT. numbers(i+1)) then
y = i
end if
end do

z = numbers(x+1)
numbers(x+1) = numbers(y+1)
numbers(y+1) = z

do i = 9,(x+1),-1
reverse(9-i+1) = numbers(i+1)
end do

do i = 0,(9-x)
j = x + 1 !mbf
numbers(j+1) = reverse(i+1)
j = j + 1
end do

str = ""
do u = 0,9
str = str // CHAR(numbers(u+1)+48)
end do

s234 = str(1:3)
s345 = str(2:4)
s456 = str(3:5)
s567 = str(4:6)
s678 = str(5:7)
s789 = str(6:8)
s8910 = str(7:9)

read(s234,*) i234
read(s345,*) i345
read(s456,*) i456
read(s567,*) i567
read(s678,*) i678
read(s789,*) i789
read(s8910,*) i8910
read(str,*) strINT
if( (modulo(i234,10)==0) .and. (modulo(i345,10)==0) .and. (modulo(i456,10)==0) .and. &
(modulo(i567,10)==0) .and. (modulo(i678,10)==0) .and. (modulo(i789,10)==0) .and. &
(modulo(i8910,10)==0) ) then

solution = solution + strINT
end if

end do
call SYSTEM_CLOCK(time_finish)
clock_finish = time_finish - time_start
write (*,"(A10,I10,//,A21,I5,A14)") "Solution: ",solution,"Program completed in ",clock_finish," milliseconds."

end program Euler43

This program is for problem 43 on the website projecteuler.net. You can see at the top of the file is where I'm declaring the str & strINT variables, and towards the end of the file is where the read() function throws the error I mentioned.

I'm wondering if the problem might have something to do with the fact that the compiler prompts me to use the -fno-range-check

$gfortran Euler43.f90 -o Euler43 -fno-range-check

RE: How to read from a string into 8-byte integer?

Another update -- Just realized now that my error was not being caused by anything related to the integer*8 declaration or the read() function itself.

I had a minor indexing problem on the do i = 0,(9-x) within the lexicographic sorting portion of the code which simply needed to have a -1 added in the index like so do i = 0,(9-x-1).

What was occurring was the substrings were being assigned invalid ASCII codes, which ended up breaking the strINT variable when concatenated.

RE: How to read from a string into 8-byte integer?

I didn't get a bug. I got the result


$ gfortran euler43.f90 -o euler43 -fno-range-check
$ ./euler43 
 Solution: 10900000000

Program completed in     0 milliseconds. 

RE: How to read from a string into 8-byte integer?

It looks like your answer was faster than mine.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close