Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CFile -> FILE* ?

0 views
Skip to first unread message

DungeonBastard

unread,
Aug 28, 2000, 3:00:00 AM8/28/00
to
What is the safest/cleanest way to get a valid FILE* from a CFile::m_hFile?

Here is the code i have been using, but it appears to leave behind a
resource leak.... (i had first done this without using _dup but that left
behind a memory leak)

What i want to be able to do is utilize an open CFile handle in code that
needs a FILE* without having to open a new file or close the existing file.
Is this possible? or should i just reopen the file by name with fopen() ?
(the CFile that would be previosly opened would ShareDenyNone)

open a CFile file
.
.
.
// Open C Runtime file handle from CFile object and open the stream
iFile = _open_osfhandle(pFile->m_hFile, _O_RDONLY);
iFile = _dup(iFile); // duplicate the file handle so when we call _fdopen
we can close it later
infile = _fdopen(iFile, "rb"); // get a valid stdio stream for the file,
now we must close with fclose
.
.
.
use the file
.
.
.
fclose(infile); // was _dup'd


-------------------
emails appreciated, but i will check back as well
e...@sunstorm.net

DungeonBastard

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to

bunny

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
Could you not use a CStdioFile or write your own wrapper which extends CFile
with a crt handle as well as an os handle in place of the CFile?

I think the reason the code you have leaks memory because you do not close the
the first crt handle you are returned via _open_osfhandle. I know of no way
either how you can close it except via _close which would close the os handle
for the the CFile too bummer...

0 new messages