 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
KL
Joined: 16 Nov 2009 Posts: 155
|
Posted: Sun Mar 24, 2013 4:13 pm Post subject: Problem with Internal File |
|
|
Dear colleagues,
What is wrong with the following program?
Code: |
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:
Best regards,
K. Lassmann |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Sun Mar 24, 2013 4:28 pm Post subject: |
|
|
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.
Code: |
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
|
_________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
KL
Joined: 16 Nov 2009 Posts: 155
|
Posted: Mon Mar 25, 2013 8:15 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Tue Mar 26, 2013 9:52 am Post subject: Re: |
|
|
KL wrote: | ... 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
Code: |
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. _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
KL
Joined: 16 Nov 2009 Posts: 155
|
Posted: Tue Mar 26, 2013 12:12 pm Post subject: |
|
|
Thank you David, I have understood the mechanism, which is more a "Convert to..." mechanism rather than an "Internal Fils" mechanism.
Klaus |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|