I am writing a package that needs to write at various times to an external file. For that I am using the \newwrite\mywriter
and \immediate\openout\mywriter
method. I know to use \closeout
when done with the file, and I do this. What I am wondering: Does LaTeX close open files, if the process exits prematurely with errors before \closeout
is reached? If it doesn't (and I think it doesn't), is there something I should do in my code to make it safer in this case, and is there something I can do to close the files before I run LaTeX again?
1 Answer
The source of TeX tells differently from your belief.
See modules 1332 and 1333 in tex.web
. Only files open for input are not closed.
-
Thank you so much for the look under the hood ... and for your patience with a newbie. (Warning: newbie taking the bait ...) so, I wonder what is different about files open for input. Nov 3, 2016 at 11:04
-
@celticflute Since the premature exit (fatal error or user's choice) can happen at an arbitrary nesting level of
\input
, it's quite hard to go back, but it's quite probable that the operating system will take care of the issue.– egregNov 3, 2016 at 11:34 -
Ah, got it. OK, I shall have faith in the OS and move on to creating a useful product (a package for building a Celtic music tunebook, BTW). Thanks again. Nov 3, 2016 at 12:32
\input
ing it later have worked fine. I just have been doing a lot of Python scripting lately, where, though the garbage collection is pretty robust, it is still not a good idea to leave files open.try ... finallly
constructs and the like are great for cleanup, but we don't seem to have such things in LaTeX. I shall not be so hesitant to drill into TeX source the next time I encounter such an input/output question.