Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: How to get a temporary file handle within mata?


From   Andrew Maurer <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   st: How to get a temporary file handle within mata?
Date   Mon, 10 Mar 2014 16:09:51 +0000

Hi Statalist,

I am writing a mata function that writes to a file using fopen(), fwrite(), and fclose(). I run into issues if the function returns an error and aborts between fopen() and fclose(). The file stays open, can't be manually deleted, and the file handle can't be accessed to fclose() it, since the file handle was a local mata object.

In Stata I would write:
tempname  fid
file open `fid' using myfile.txt
---- code ----
file close `fid'

That way, if the program aborts in ---- code ----, the file is automatically closed. Is there a similar method that may be used within mata? The only workaround I have so far is to use st_tempfilename() to start writing to a temporary file, and at the end of the function overwrite the actual file with the temporary file.

Thank you,

Andrew Maurer 



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index