Re: Check whether a file handle is open
"Faisal" <faisalm83@gmail.com> ha scritto nel messaggio
news:e98b893d-b0eb-4628-9058-588542ba7150@x1g2000prh.googlegroups.com...
But how i could avoid multiple closing of a file handle.
I would wrap the file handle into a C++ class, e.g. FileHandle.
In the destructor of this class, you close the file.
(I read in your initial post that you open the file with _open, so your
handle is an 'int'.
So, you can put a member variable of type 'int' in your C++ class, make it
private, and expose a public accessor to that handle.
In the destructor, you can use _close to close the file.)
Instead of using the "raw" 'int' file handle, you can use this C++
'FileHandle' class. You can wrap this class using a smart pointer like
shared_ptr.
So, in your code, you refer to FileHandle using the shared_ptr smart
pointer.
shared_ptr automatically manages a reference count internally; so, when
there is no more "client" using the file handle class, the destructor of
your 'FileHandle' class is called, and the file handle is closed.
Smart pointers like shared_ptr are good tools for managing non-memory
resources (like file handles, as in your case, or textures, etc.).
HTH,
Giovanni