Converting character string to integer

浪尽此生 提交于 2019-12-02 06:47:22

问题


This is a follow up to my get_command_argument() question.

I'm reading a command line argument (arg) into a Fortran program. Then I want to store the value of arg as an integer. ichar() doesn't do the job.

This seems kind of basic, so clearly I'm doing something wrong. Any hints?

program test_get_command_argument
   integer :: i,j
   character(len=32) :: arg

   i = 0
   do
       call get_command_argument(i,arg)
       if (LEN_TRIM(arg) == 0) EXIT

       write (*,*) trim(arg)
       i = i + 1
   end do

   j = ichar(arg)


end program

回答1:


You want to use the "internal files" capability. You should have a statement like read(arg,*) j. This will read the character variable arg as if it were a file and store the result into j.




回答2:


This isn't an answer but an extended comment:

That's a bizarre way to loop over the command line arguments. What's wrong with the straightforward and obvious

do i = 1, command_argument_count()
   call get_command_argument(i,arg)
   ! do funky stuff    
end do


来源:https://stackoverflow.com/questions/19877812/converting-character-string-to-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!