I having a problem here.
I'm trying to append data to a CFile using FILE*
But when execute the application, it always give error saying "No such file or directory".
I can actually see the file created but it just keep giving error "No such file or directory".
Is the file being lock or the file just created so it can not be find by fstream(FILE*)?
or the file mode is wrong?
Can somebody help me with this:
Below is the code:
[QUOTE]
void SaveDocument(CString strFile)
{
CFile file;
if( !file.Open(strFile, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
{
CArchive ar(&file, CArchive::store);
// save now.
MyClass.Serialise(ar);
ar.Flush();
ar.Close();
file.Close();
}
}
void MyClass::Serialize(CArchive &ar)
{
if(ar.IsStoring())
{
ar << parameter1;
ar << parameter2;
...
ar.Flush();
CFile *pFile = ar.GetFile();
int fd = _open_osfhandle((long)(pFile->m_hFile), _O_APPEND); // fd is > 0
FILE *pFILE = NULL;
pFILE = _wfdopen(fd, _T("a+"));
CString str;
str = strerror(errno);
AfxMessageBox(str); // <=Here always give "No such file or directory"
}
}
[\QUOTE]
Attached is the printscreen of the FILE* pointer. the pointer is evaluated as bad pointer.
Why the FILE* pointer not able point to the file being created?
I need to do that cause a third party library's save function needed the FILE* handle.
This is why i needed to call this function:
pFILE = _wfdopen(fd, _T("a+"));
Dear VictorN,
If the CArchive is not the same, when i run in debug mode. The strFile name is correct.
I'm passing the ar by reference. i did not see any wrong with my code.
Please correct me if i'm wrong.
Thanks.
I need to do that cause a third party library's save function needed the FILE* handle.
This is why i needed to call this function:
pFILE = _wfdopen(fd, _T("a+"));
Then how about CFile::m_hFile? How about opening the file after Serialize is done? I don't see why it would need to be the same file handle the archive is using.
You want to append in Serialize using a different method. But you know, the data you put in the archive might not be flushed to the file already, so your append might not be at the end, even if it worked.
If a CRT function succeeds it does NOT obviously set errno to 0 ("No error").
According to documentation, _fdopen / _wfdopen returns NULL in case of failure or a valid FILE* pointer otherwise.
So, you have to test first the returned value before getting the error description.
I have tested the strerror(errno) message. It seem to store the previous error message which confuse me.
I thought the error message was for my _fdopen() function. Is actually not.
refer to ovidiucucu,
If a CRT function succeeds it does NOT obviously set errno to 0 ("No error").
That is correct.
My function is correct.
Thanks for solving my problem.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.