Silverfrost Forums

Welcome to our forums

Problem with Internal File

24 Mar 2013 3:13 #11874

Dear colleagues,

What is wrong with the following program?

Winapp

    Program Test79

      Implicit None

      Integer           , Dimension (10) :: n
      Integer                            :: i
      Character (len=20), Dimension (10) :: Buffer

      Buffer (1) = '1 , 2'
      Buffer (2) = '3 , 4, 5'
      Buffer (3) = '6'

      Read ( Buffer, * ) ( n (i), i = 1,5 )
      Read ( Buffer, * )   n (6)

      write (*,*)        ( n (i), i = 1,5 )
      write (*,*)          n (6)
       
!     ----------------------------------------

      Open (Unit = 10, File = 'Input')

      Read  ( 10, * ) ( n (i), i = 1,5 )
      Read  ( 10, * )   n (6)

      write (*,*)     ( n (i), i = 1,5 )
      write (*,*)       n (6)
 
    End Program Test79

The program reads Buffer (1) and Buffer (2) correctly by an implied-do loop but fails reading Buffer (3). If Buffer is put into a file, reading is (of course) correct.

File Input:

1 , 2      
3 , 4, 5   
6         

Best regards,

  1. Lassmann
24 Mar 2013 3:28 #11875

The second read just starts over at the start of the contents of the buffer array. There is only one record with internal files.

If you want to read different array elements you can use code like the following.

Winapp

    Program Test79

      Implicit None

      Integer           , Dimension (10) :: n
      Integer                            :: i
      Character (len=20), Dimension (10) :: Buffer

      Buffer (1) = '1 , 2'
      Buffer (2) = '3 , 4, 5'
      Buffer (3) = '6'

      Read ( Buffer(1), * ) ( n (i), i = 1,2 )
      Read ( Buffer(2), * ) ( n (i), i = 3,5 )
      Read ( Buffer(3), * )   n (6)

      write (*,*)        ( n (i), i = 1,5 )
      write (*,*)          n (6)
       
!     ----------------------------------------

      Open (Unit = 10, File = 'Input')

      Read  ( 10, * ) ( n (i), i = 1,5 )
      Read  ( 10, * )   n (6)

      write (*,*)     ( n (i), i = 1,5 )
      write (*,*)       n (6)
 
    End Program Test79 
25 Mar 2013 7:15 #11880

Thank you very much, David. Obviously, I focussed too much on the wording 'Internal File' (you could call it wishful thinking) and the possibility that internal file may be an array, where its elements are treated as records. Unfortunately, I missed the restriction you mention that after any I/O operation the internal file is always positioned at the beginning of the first record. Thanks again for the prompt help, Klaus

26 Mar 2013 8:52 #11886

Quoted from KL ... the possibility that internal file may be an array, where its elements are treated as records. Klaus

You can use arrays of characters in this way. But the records need to be accessed in one read or write operation. For example, the following code uses the / editing to read from each record

character(len=10) :: buffer(4)
buffer(1) = '1'
buffer(2) = '2'
buffer(3) = '3'
buffer(4) = '4'
read(buffer,'(i5,/)') i, j, k, l, m
end

If you want multiple reads (for example) you need to use the index to the starting record (element) you want.

26 Mar 2013 11:12 #11887

Thank you David, I have understood the mechanism, which is more a 'Convert to...' mechanism rather than an 'Internal Fils' mechanism.

Klaus

Please login to reply.