1

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
2
  • got it here, gee, Fortran is a lot different from C ;-) Commented Nov 9, 2013 at 15:54
  • @HighPerformanceMark doesn't command_argument_count() just return the number of arguments (I read that here), but I'd still like to work with the actual values of the arguments. Commented Nov 9, 2013 at 17:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I just tried that. I put read(arg,'(I32)') j in the line after the call get_command_argument(), but at runtime I get the error: Bad integer for item 1 in list input
got it, I am so used to argc and argv in C, that I totally missed the point that the first argument in get_command_argument() is actually the calling of the executable, duh. :-)
Depends on what you mean by first argument...fortran is generally 1-based, not 0-based as C is. get_command_argument is the only exception I've seen, where 0 means the name of the program and 1 means the first argument sent to the program.
yup, it's the only exception I've seen so far (in my very short fortran career) as well ;-)
1

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

1 Comment

I agree. In found this example on the gcc.gnu.org website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.