Silverfrost Forums

Welcome to our forums

WRITE returns different values depending on target

3 Nov 2017 9:50 #20642

Good evening, Everyone,

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

when using

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!

4 Nov 2017 12:26 #20643

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=*.

 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

4 Nov 2017 2:07 #20645

To better understand read (lu,), I also tried : integer lu, iostat character line80

  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 '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

6 Nov 2017 8:00 #20680

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.

Please login to reply.