replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Problem opening and writing to files
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 

Problem opening and writing to files

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



Joined: 21 Jul 2011
Posts: 8

PostPosted: Fri Jul 22, 2011 9:38 am    Post subject: Problem opening and writing to files Reply with quote

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
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Jul 22, 2011 9:47 am    Post subject: Reply with quote

Try as the compiler suggests;

OPEN(11,FILE='PEF.txt')
OPEN(12,FILE='uvw101_NEW.txt')
CLOSE(11)
CLOSE(12)
Back to top
View user's profile Send private message
JosephHarris



Joined: 21 Jul 2011
Posts: 8

PostPosted: Fri Jul 22, 2011 1:08 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Jul 22, 2011 2:29 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Fri Jul 22, 2011 5:55 pm    Post subject: Reply with quote

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.

Code:

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

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
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