开发者

Can the input to a FORTRAN 'READ' statement be taken from a string literal?

For the purpose of developing some tests, it would be helpful if I could input to a FORTRAN READ statement using a string literal in the source rather than an external file or stdin. Python has a StringIO module which can be used to generate a file-like object from a string literal, is something similar possible in FORTRAN?

i.e.

      MAKEFAKEDEVICE(N, '开发者_运维知识库Some string literal here')
      READ(N, '(A)'), VAR1


You can use an internal read or write (that is, IO from/to variables of type CHARACTER vs. files for regular IO):

!Demonstrate internal read/write                                                                                             
program intio                                                                                                                
  implicit none                                                                                                              
  character(len=20) :: a, b                                                                                                  
  a = "hello world!"                                                                                                         
  ! Read a into b with A format                                                                                              
  read(a, '(A)') b                                                                                                           
  print *, b  ! Should print "hello world!"                                                                                  
  ! Now write into b                                                                                                         
  write(b, *) "I said hello!"                                                                                                
  print *, b                                                                                                                 
  ! Read into b from literal                                                                                                 
  !read("Well, hello!", '(A)') b                                                                                             
  !print *, b                                                                                                                
end program intio                                                                                                            

If you uncomment the last two lines you get

intread.f90:13.7:

  read("Well, hello!", '(A)') b
       1
Error: UNIT specification at (1) must be an INTEGER expression or a CHARACTER variable

So no, you can't read from a literal.


I have just taken a look at the tests used by the GCC project and it seems that the following is valid,

      OPEN(10, STATUS='scratch')
      WRITE(10, '(A)'), 'Some literal'
      REWIND(10)
      READ(10, '(A)') VAR1


I really don't know (and it's too early for me to start thinking :) if this is of any help, but you could try searching in your help for "internal read".

I don't know of any way to read a part of the source, but a regular string could be read by something like

string = 'some literal'  
read(va1, (a))string  

http://www.megasolutions.net/fortran/internal-read-of-character-array_F-ifort-differences-77847.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜