 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
Magliola
Joined: 10 Feb 2014 Posts: 8 Location: Chicago, Illinois USA
|
Posted: Sun Feb 16, 2014 7:55 pm Post subject: Write(*,*) and Print * Statements |
|
|
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. _________________ Regards,
Bob |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Mon Feb 17, 2014 3:03 am Post subject: |
|
|
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.
Code: | 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 |
|
|
Back to top |
|
 |
Magliola
Joined: 10 Feb 2014 Posts: 8 Location: Chicago, Illinois USA
|
Posted: Mon Feb 17, 2014 5:54 am Post subject: |
|
|
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 (*, *). _________________ Regards,
Bob |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Mon Feb 17, 2014 9:28 am Post subject: |
|
|
Units 1,2,5 and 6 are reserved for standard input and output. |
|
Back to top |
|
 |
Magliola
Joined: 10 Feb 2014 Posts: 8 Location: Chicago, Illinois USA
|
Posted: Mon Feb 17, 2014 12:10 pm Post subject: |
|
|
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? _________________ Regards,
Bob |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2615 Location: Sydney
|
Posted: Mon Feb 17, 2014 11:59 pm Post subject: |
|
|
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
Code: | 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 ! |
|
Back to top |
|
 |
Magliola
Joined: 10 Feb 2014 Posts: 8 Location: Chicago, Illinois USA
|
Posted: Tue Feb 18, 2014 3:38 am Post subject: |
|
|
John, Thank you for your help and advice. I am definitely adopting your practice to unit numbering. _________________ Regards,
Bob |
|
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
|