I am trying to use a FILE pointer multiple times through out my application for this I though I create a function and pass the pointer through that. Basically I have this bit of code
FILE* fp;
_wfopen_s (&fp, L"ftest.txt", L"r");
_setmode (_fileno(fp), _O_U8TEXT);
wifstream file(fp);
which is repeated and now instead I want to have something like this:
wifstream file(SetFilePointer(L"ftest.txt",L"r"));
....
wofstream output(SetFilePointer(L"flist.txt",L"w"));
and for the function :
FILE* SetFilePointer(const wchar_t* filePath, const wchar_t * openMode)
{
shared_ptr<FILE> fp = make_shared<FILE>();
_wfopen_s (fp.get(), L"ftest.txt", L"r");
_setmode (_fileno(fp.get()), _O_U8TEXT);
return fp.get();
}
this doesn't simply work. I tried using &*fp instead of fp.get() but still no luck.
You aren't supposed to create
FILEinstances withnewand destroy them withdelete, likemake_shareddoes. Instead,FILEs are created withfopen(or in this case,_wfopen_s) and destroyed withfclose. These functions do the allocating and deallocating internally using some unspecified means.Note that
_wfopen_sdoes not take a pointer but a pointer to pointer - it changes the pointer you gave it to point to the newFILEobject it allocates. You cannot get the address of the pointer contained inshared_ptrto form a pointer-to-pointer to it, and this is a very good thing - it would horribly break the ownership semantics ofshared_ptrand lead to memory leaks or worse.However, you can use
shared_ptrto manage arbitrary "handle"-like types, as it can take a custom deleter object or function:Please do take a look at the link @KerrekSB gave, it covers this same idea with more detail.