Silverfrost Forums

Welcome to our forums

Problem opening and writing to files

22 Jul 2011 8:38 #8606

Hello,

I did a quick search and couldn't find an answer to this problem, which i believe to be fairly simple and hopefully will receive a quick and easy answer!

I am trying to compile and run a code in which a file full of data is opened and the data is used to compute some more values which are then put into a different file. So that is two separate files which have to be opened, read, written and then closed.

For this i have used the commands:

OPEN(1,FILE='PEF.txt')

OPEN(2,FILE='uvw101_NEW.txt')

CLOSE(1)

CLOSE(2)

etc.

But when i try and compile, i get the warning that

  • 'opening unit 1 may affect the operation of input from the default unit '*' - are you sure you want to do this?' And similarly for unit 2.

It compiles, but when i run it, the output file contains no data, so i'm presuming the program had trouble either reading from the input file or writing to the output?

Can anyone tell me why this is? Is there something wrong with my syntax? It seems like a fairly common issue, but i couldn't find and solution to it.

Thanks in advance!

Joe

22 Jul 2011 8:47 #8607

Try as the compiler suggests;

OPEN(11,FILE='PEF.txt') OPEN(12,FILE='uvw101_NEW.txt') CLOSE(11) CLOSE(12)

22 Jul 2011 12:08 #8609

Aaaaah, thanks.

That worked perfectly. I still don't understand exactly how the warning message relates to the answer; it wouldn't occur to me that that message meant 'use a double digit for the unit number', but that doesn't matter, it works now.

Thank you very much!

22 Jul 2011 1:29 #8610

Histoically, unit numbers 1,4,5,6 & 7 have been special unit numbers for past fortran compilers. This is still the case today with some compilers. FTN95 reserves unit 1 as equivalent to reading or writing to the screen/console, while Lahey has reserved units 5,6 & 7 for reading, writing and errors. Unit 4 was once the tape drive !!

My safe approach is to use unit numbers > 10, ensuring there is no conflict with possible reserved unit numbers. I'm not sure what the maximum possible number is or how many files can be open at the same time, but if you start at 11, it is unlikely you will run out of possible numbers. (Numbers > 100 would probably work, but I've never tried this in over 30 years of using fortran!) I have standardised unit = 98 as my program log file for miscelaneous run messages. It's the first file I open. I am yet to have a program which opens so many files that I can't find an available number between 11 and 97. If I needed to open more than 90 files, I would not open them all at the one time. Earlier versions of fortran had limits on the number of files you could have open, as low as 10 from memory, but did not apply the same limit to the value of the unit number.

It's also a good habit to use 'IOSTAT=...' in all I/O statements (especially OPEN and READ) and test the value, reporting if it is not 0. There are lots of possible reasons for I/O errors, which should be corrected.

John

22 Jul 2011 4:55 #8611

You can get a list of valid unit numbers (and which ones are pre-connected) using the following code. This lists valid unit numbers between 1 and 99, which with FTN95 is all of them (you can also go higher than 99).

It also reports that units 1,2,5,6 are pre-connected and should be avoided.

I personally try to use numbers between 10 and 60 (I rarely need more than several files open at once). The lower limit 10 is for the reason John reports; the upper limit is the limit that existed on my first Fortran 77 compiler and I preserve it out of habit.

program list_units

   logical :: is_valid, is_preconnected
   integer :: n, num_valid

   num_valid = 0
   do n=1,99
      inquire(unit=n,exist=is_valid)
      if (is_valid) then
         num_valid = num_valid + 1
         inquire(unit=n,opened=is_preconnected) 
         if (.not. is_preconnected) then
            write(*,'('Unit number ',i0,' is valid')')  n
         else
            write(*,'('Unit number ',i0,' is valid and pre-connected')')  n
         end if
      end if
   end do
   write(*,'(/'Number of Valid units is ',i0)') num_valid

end program list_units
Please login to reply.