Hi Chris,
This is a consequence of the fact that arguments are passed by value, not
reference. So fclose cannot modify the variables f0 and f1, just the versions it
receives on the stack.
I expect that what happens when writing using a closed file desciptor is
C-library specific.
If you think this may be a problem, then the work around is to write
fclose(f0); f0 = 0;
or, using the fact that Ox fclose returns 0:
f0 = fclose(f0);
A extension to Ox would be to allow fclose(&f0), which would be short-hand for
the above. This would not work in the following sketched example though:
MyNicePrinting(fo)
{
fprint ...
// all done:
fo = fclose(fo);
}
decl fo = fopen(...);
MyNicePrinting(fo);
I would not write code like that, because I prefer to match fopen and fclose
more clearly in the code.
The only other way that I can see is to change the Ox file value into an index
into an internally tracked array. This would be quite a substantial change.
Best wishes, Jurgen
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
oxoxoxox 18th OxMetrics user conference 2016
oxoxoxox Cass Business School, 12-13 Sept
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
Dr Jurgen A Doornik
James Martin Fellow, Institute for New Economic Thinking
at the Oxford Martin School, University of Oxford
http://www.doornik.com
www.doornik.com
Jurgen A. Doornik (D.Phil. Oxon) is a James Martin Fellow at the Institute for New Economic Thinking at the Oxford Martin School of the University of Oxford, and a director of OxMetrics Technologies Ltd.
|
http://www.oxmetrics.net
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
On 2016-08-08 16:22, Chris Ferrall wrote:
> I've encountered a bug that the simple program below partly illustrates.
>
> After fclose(f) I would expect fprintln(f,...) to produce an error. It does not, because f is still considered a file after close. The program runs but nothing is sent to file1.txt. To produce an error you also have to do something like "f = 0;" after
the fclose() so that f is no longer of type file.
>
> The real error in my real program that I can't duplicate with a simple program yet is that the text sent to f1 actually appears in another open file, like f0! The real code that produces this effect involves sending a file as an argument to a function and
other stuff that will take a while to strip down.
>
> I will work around the issue, but maybe this is enough for Jurgen to track down the issue.
>
> ----------------------------
>
> #include "oxstd.h"
> static decl f0, f1;
> main() {
> f0 = fopen("file0.txt","w");
> f1 = fopen("file1.txt","w");
> fprintln(f0,"Send something to f0");
> fclose(f1);
> fprintln(f1,"Should produce error, but does not. And under some conditions appears in file0.txt");
> fclose(f0);
> }
>