September 21, 2007

Addition to: How to write Textfiles with encoding in MFC

Martin Richter pointed me to a problem in my previous posted code-example regarding opening files with a specific encoding.
If you pass a FILE* stream to the CStdioFile constructor, then it will not auto-delete the file in the destructor!

To prevent this, you can use a derived class:

class CStdioFileWithClose : public CStdioFile { public: CStdioFileWithClose(FILE *pOpenStream) : CStdioFile(pOpenStream) { m_bCloseOnDelete = TRUE; } };

Then the example should look like:

// Open the file with the specified encoding FILE *fStream; errno_t e = _tfopen_s(&fStream, _T("test.txt"), _T("wt,ccs=UNICODE")); if (e != 0) return; // failed.. CStdioFileWithClose f(fStream); // open the file from this stream f.WriteString(_T("Test")); f.Close();

Posted 1 month ago on September 21, 2007
The trackback url for this post is

Comments have now been turned off for this post