Reading a string with spaces in Fortran

后端 未结 2 1707
日久生厌
日久生厌 2021-02-20 09:14

Using read(asterisk, asterisk) in Fortran doesn\'t seem to work if the string to be read from the user contains spaces. Consider the following code:

    characte         


        
相关标签:
2条回答
  • 2021-02-20 09:57

    Instead of read(*, *), try read(*, '(a)'). I'm no Fortran expert, but the second argument to read is the format specifier (equivalent to the second argument to sscanf in C). * there means list format, which you don't want. You can also say a14 if you want to read 14 characters as a string, for example.

    0 讨论(0)
  • 2021-02-20 10:05
      character(100) :: line
    
      write(*,'("Enter some text: ",\)')
      read(*,'(A)') line
      write(*,'(A)') line
    
      end
    

    ... will read a line of text of maximum length 100 (enough for most practical purposes) and write it out back to you. Modify to your liking.

    0 讨论(0)
提交回复
热议问题