For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2005 > Re: I/O error in subroutine does not always produce error messages?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: I/O error in subroutine does not always produce error messages?
AN O'Nymous

2005-12-05, 8:08 am

Hmm, turns out I didn't quite find the bug as I thought. It was
intermittently re-appearing in subsequent runs.

Following the key part of your advice that file unit numbers are
global, I removed redundant "open" statements and replaced later
"close-open" pairs with "rewind".

Everything works OK now :)

Not quite sure what the bug is but that is how I fixed it!

Richard Maine

2005-12-05, 7:04 pm

AN O'Nymous <a_n_onymous80@yahoo.co.uk> wrote:

> No, I didn't have an IOSTAT command.


(It is a specifier rather than a command.)

> I will include the following lines in future code...(let me know if I
> got the syntax wrong)
>
> Open (1,file="input", iostat="temp_integer")
> Read (1,'(a10,e10.4)', temp_integer)
> Print *,"IOSTAT =", temp_integer


A couple of things wrong here.

In the Open statement, don't quote the variable name. Just
iostat=temp_integer instead of iostat="temp_integer".

And in the read statement, you omitted the iostat=. Again, it should be
iostat=temp_integer.

I get the impression that perhaps you think there is some relationship
between the iostat in the open and read statements. Perhaps I'm just
misreading the signs, but anyway, there isn't. The iostat= specifier
gives a variable that is used to report the status of the particular
statement. Unlike the unit number, the iostat= variable is not global.
It applies only to a single statement. If you want iostat information
from both the open and read statements above, they are completely
independent. You can use the same variable or a different one.

Note that if you use the same variable, you need to check it after the
open and then again after the read. That's because any value from the
read will just overwrite whatever value came back from the open. They
won't be added somehow.

While iostat is very useful in general, I'm not sure that it is so for
what you want. Generally speaking, you use iostat= to get status
information and then comntinue on. For example, your program might have
code to handle some kinds of errors, so you want to continue and execute
yourt error-handling code instead of aborting on the error.

If all you want to do is get an error message and abort, then you
generally do *NOT* want to use iostat. (Well, I've done it when the
error message needs to go somewhere unusual, like to a log file or
separate client program on another machine, but that's unusual). In
fact, iostat= can be counterproductive in that you *DON"T* get an error
message with it. All you get is a numeric code, which you then have to
figure out how to interpret (interpreting the codes is a fairly frequent
question here and is compiler-dependent). F2003 adds a much-needed
facility to get the error message text. On occasion (f2003 compilers not
yet being here), I've been known to take the iostat= out of a statement
in order to get the error messages instead.

If you aren't getting an error messsage without iostat=, then adding an
iostat= isn't likely to get you one.

One thing to watch out for iostat= is that if you specify it in an I/O
statement, you really need to check its value afterwards. If you specify
it and don't check its value, you won't notice when there are errors -
the results will just be "funny". The iostat= specifically says for the
compiler not to abort and put out an error message - you will handle the
error. But then you don't handle it either.

Afraid I don't have enough data to be of more constuctive help about
your problem.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Jugoslav Dujic

2005-12-05, 7:04 pm

AN O'Nymous wrote:
| No, I didn't have an IOSTAT command. I didn't know about it until you
| mentioned it, but after reading the manual about it, it certainly
| sounds like a useful command.
|
| I will include the following lines in future code...(let me know if I
| got the syntax wrong)
|
| Open (1,file="input", iostat="temp_integer")
| Read (1,'(a10,e10.4)', temp_integer)
| Print *,"IOSTAT =", temp_integer

You did; iostat variable should already be an integer.

Besides, since you're expecting the file to already exist, it's
good to add the qualifiers:

Open (1,file="input", STATUS="old", ACTION="read", iostat=temp_integer)
If (temp_integer /= 0)
Print *,"Cannot open file 'input'. Reason: ",temp_integer
Else
Read(1,...

I didn't follow the rest of the thread carefully, so note that
ACTION="read" might clash with potential usages
of REWIND and write. personally, I prefer closing and re-opening
to rewinding, but YMMV.

--
Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com