Silverfrost Forums

Welcome to our forums

Write(*,*) and Print * Statements

16 Feb 2014 6:55 #13720

Does Silverfrost FTN95 allow statements such as

   print *, ' This is a test '  

and

   write(*, 100)

100 format(1x,' This is a test')

Sometimes print * and write (*, ) works and sometimes I get the following error at run time:

*** Error 94, Unit has neither been OPENed nor preconnected

Also I cannot find where print * and write (*, ) are documented in Silverfrost FTN95 documentation.

(I am porting over code from a compiler where the above statements are valid.)

Thanks to anybody that helps with this.

17 Feb 2014 2:03 #13721

You should not get the error report you have identified for the code examples you have provided. I typically use write (,) 'This is a test'

where you would probably get the error report is when you have: write (1,) 'This is a test' or write (6,) 'This is a test' In these cases, the code was written where fortran unit 1 or 6 defaulted to standard output. Salford Fortran defaults unit 1, although 5 and 6 also work. This might not be the case with other compilers. The following example demonstrates what works with FTN95.

    integer*4 lu, iostat
!
    do lu = 1,10
      write (lu,*, iostat=iostat) 'successful test for unit',lu
      if (iostat /= 0) write (*,*) 'ERROR : unsuccessful test for unit',lu
    end do
    write (lu,*) 'error example for unit',lu
!
end
17 Feb 2014 4:54 #13722

John, Thank you for the help and post. I ran your bit of code and I get the following:

successful test for unit 1 successful test for unit 2 ERROR : unsuccessful test for unit 3 ERROR : unsuccessful test for unit 4 successful test for unit 5 successful test for unit 6 ERROR : unsuccessful test for unit 7 ERROR : unsuccessful test for unit 8 ERROR : unsuccessful test for unit 9 ERROR : unsuccessful test for unit 10

It looks like units 1, 2, 5 and 6 all default to output to the console window.

I did a little testing and I see that if units 1 through 10 are all first assigned to files then write(,) and print * both write to the file attached to unit 2. So * and 2 are synonymous.

Is there a default unit for reading from the keyboard?

I am writing short test programs to try replicating the error in the ported program. But no luck so far. (i.e. I cannot duplicate the error.)

Any ideas why print * and write (*, *) works and sometimes I get the following error at run time:

*** Error 94, Unit has neither been OPENed nor preconnected

It looks like error 94 should never happen with print * and write (*, *).

17 Feb 2014 8:28 #13723

Units 1,2,5 and 6 are reserved for standard input and output.

17 Feb 2014 11:10 #13724

Thank you John and thank you Paul for helping me solve this code problem.

For reference, the symptoms the code had and the error are thus:

  1. WRITE (,) works as expected with output to the console window.
  2. the code then opens unit 2 as a scratch file.
  3. WRITE (,) now writes to the scratch file. [That explains why the write (,) stopped writing to the console window.]
  4. the code then executes CLOSE(2,STATUS='KEEP'). The next occurrence of WRITE (,) crashes the program with the Error 94.

Follow up Question: In good practice how should one take advantage of units 1,2,5 and 6 in their code?

17 Feb 2014 10:59 #13725

My approach to good practice is to never open a file using a file unit number less than 11. (11 for read, 12 for write) This copes with all fortran compilers I have ever used. I typically open text files for units 11-19, binary files on 21+ and a log file on unit 98. This approach has been based on once using a compiler that limited the file number to 99. I am not sure what the maximum number can be, but I have seen examples of unit numbers 1000+. I have never realy had to find out. Over the years, there have been many posts where people got in trouble with 'reserved' file unit numbers < 10. I did know that units 1,5 and 6 were often special but have never had the need to find out that unit 2 is also reserved in FTN95. There's always somenthig new.

John

    integer*4 lu, iostat
    character line*80
!
    do lu = 1,10
      write (lu,*, iostat=iostat) 'successful test for unit',lu
      if (iostat /= 0) write (*,*) 'ERROR : unsuccessful test for unit',lu
    end do
!    write (lu,*) 'error example for unit',lu
!
    do lu = 11,11011,500
!
      open (unit=lu, file='some_file.txt', iostat=iostat)
       if (iostat/=0) then
         write (*,*) 'unable to use file unit',lu
         cycle
       end if
!
      if (lu==11) then
       write (11,*) 'test line written using file unit',lu
      else
!
       read (lu,fmt='(a)', iostat=iostat) line
        if (iostat/=0) then
         write (*,*) 'unable to read file unit',lu
        else 
         write (*,*) lu,' ',trim (line)
        end if
      end if
!
      close (unit=lu,iostat=iostat)
       if (iostat/=0) then
         write (*,*) 'unable to close file unit',lu
       end if
!
    end do
!
end

The limit must be a big number !

18 Feb 2014 2:38 #13726

John, Thank you for your help and advice. I am definitely adopting your practice to unit numbering.

Please login to reply.