forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WRITE returns different values depending on target

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
viroxa



Joined: 28 Jul 2017
Posts: 78

PostPosted: Fri Nov 03, 2017 10:50 pm    Post subject: WRITE returns different values depending on target Reply with quote

Good evening, Everyone,

I'm having a problem with WRITE, which I can't put my finger on:

when using
Code:
DO icoun=1,UBOUND(array)
!   Print results
    WRITE(*,'(F20.15)')icoun,array(icoun))       
END DO

(with * referring to the command shell), the expected values are displayed.

When substituting * with the number of the desired target file, only 0's are written to the file.

Any ideas, what I'm missing?

Thanks!
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 1846
Location: Sydney

PostPosted: Sat Nov 04, 2017 1:26 am    Post subject: Reply with quote

You have a few problems with your posted code.
Try:
WRITE(*,'(I0,1x, F20.15)') icoun, array(icoun)

The following code may give a clue to a related problem using read ( fmt=*.
Code:
 real, allocatable :: array(:)
 integer n, icoun, lu, iostat
 character line*80
 
  n = 20
  allocate ( array(n) )
  call random_number ( array )
 
  write (*,*) 'Print results'
  DO icoun=1,UBOUND(array,1)
     WRITE(*,'(i0,1x,F20.15)') icoun, array(icoun)
  END DO

  write (*,*) 'OPEN file'
  lu = 11
  open (unit=lu, file='array.txt' )

  write (*,*) 'Write results to file'
  DO icoun=1,UBOUND(array,1)
     WRITE (lu,'(i0,1x,F20.15)') icoun, array(icoun)
  END DO
  close (lu)

  write (*,*) 'Read results from file using fmt=*'
  open (unit=lu, file='array.txt' )
  do
    read (lu, fmt=*, iostat=iostat) line
    if ( iostat /= 0) exit
    write (*,*) trim (line)
  end do
  close (lu)

  write (*,*) 'Read results from file using fmt=(a)'
  open (unit=lu, file='array.txt' )
  do
    read (lu, fmt='(a)', iostat=iostat) line
    if ( iostat /= 0) exit
    write (*,*) trim (line)
  end do

  end


Note : Read ( * is terminated by a blank
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 1846
Location: Sydney

PostPosted: Sat Nov 04, 2017 3:07 am    Post subject: Reply with quote

To better understand read (lu,*), I also tried :
Code:
 integer   lu, iostat
 character line*80
 
  lu = 11

  write (*,*) 'Read results from file using fmt=*'
  open (unit=lu, file='array-quote.txt' )
  do
    read (lu, fmt=*, iostat=iostat) line
    if ( iostat /= 0) exit
    write (*,*) trim (line)
  end do
  close (lu)

  end


file array-quote.txt is
Code:
"1    0.673070371150970"
"2    0.330984950065613"
"3    0.147484719753265"
"4    0.741447806358337"
"5    0.344161212444305"
"6    0.089860200881958"
7    0.736448347568512
8    0.523410677909851
9    0.614522159099579
10    0.670769095420837
11    0.005530893802643
12    0.750596880912781
13    0.542138993740082
14    0.423445820808411
15    0.435072660446167
16    0.548545062541962
17    0.265481770038605
18    0.089840590953827
19    0.159591436386108
20    0.728632807731628
Back to top
View user's profile Send private message
viroxa



Joined: 28 Jul 2017
Posts: 78

PostPosted: Mon Nov 06, 2017 9:00 pm    Post subject: Reply with quote

John,

Thanks for your reply. In my actual code I have '(I0,1x, F20.15)' included. I must have forgotten it, when typing the post.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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