|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
file open and close
tried a small program: open(1,file='test.txt') close(2) end compiled well & ran with g95 & gfortran without errors. one way to avoid that would be: integer funit funit=1 open(funit,file='test.txt') close(funit) end what are expers comments ?
On Apr 19, 10:20 pm, Magician <jadoo.d@gmail.com> wrote:
> tried a small program: > open(1,file='test.txt') > close(2) > end > compiled well & ran with g95 & gfortran without errors. one way to > avoid that would be: > integer funit > funit=1 > open(funit,file='test.txt') > close(funit) > end > what are expers comments ?
You might want to use a named constant instead. For example, IMPLICIT NONE INTEGER UNITNO PARAMETER (UNITNO=1) OPEN(UNITNO, FILE='test.txt') CLOSE(UNITNO) END Bob Corbett
Magician <jadoo.d @gmail.com> wrote: > tried a small program: > open(1,file='test.txt') > close(2) > end > compiled well & ran with g95 & gfortran without errors.
That's because it is valid. Not exactly as intended, perhaps, but valid. It is ok to close a unit that isn't opened (which does nothing), and it is ok to end the program without closing a file (it is implicitly closed). I recommend explicitly closing files, but it isn't an error to not do so. > one way to > avoid that would be: > integer funit > funit=1 > open(funit,file='test.txt') > close(funit) > end > what are expers comments ?
Well, there are many reasons to use variables for unit numbers anyway. Yes, one of those reasons is that you are less likely to miss some cases if you need to change the number for some reason. There are quite a large host of other reasons as well. Too late for me to try to elaborate much. But the summary is that, yes, that's an improvement. While you are on the subject, it is usually recommended to avoid unit numbers less than about 10. The single-digit unit numbers are sometimes reserved for system-dependent purposes. They might work fine on some compilers, but not on others. Of course, if you are using variables for your unit numbers, it is easy to change; you only have to change it in one place. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
On Apr 20, 1:20 am, Magician <jadoo.d@gmail.com> wrote:
> tried a small program: > open(1,file='test.txt') > close(2) > end > compiled well & ran with g95 & gfortran without errors. one way to > avoid that would be: > integer funit > funit=1 > open(funit,file='test.txt') > close(funit) > end > what are expers comments ?
In an OPEN statement I suggest always specifying the ACTION, for example "read" or "write", in order to (1) clarify the intent of the code (2) prevent unintended actions later in the program, for example writing to a file that was intended to be read (and therefore overwriting it).
|
 |
 |
 |
 |
|