1 Attachment(s)
Append Data to CFile via FILE*
Dear All,
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?
Thanks.
Re: Append Data to CFile via FILE*
Why are you trying to do that? Why not just use CArchive::Write, or perhaps use the CFile to write.?
Re: Append Data to CFile via FILE*
Quote:
Originally Posted by
VbEndUser
But when execute the application, it always give error saying "No such file or directory".
It looks like CArchive instance you pass in MyClass::Serialize is not the same that you used in SaveDocument function.
Re: Append Data to CFile via FILE*
Dear GCDEF,
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.
Re: Append Data to CFile via FILE*
Quote:
Originally Posted by
VbEndUser
Dear GCDEF,
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.
Re: Append Data to CFile via FILE*
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.
Re: Append Data to CFile via FILE*
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.
Code:
FILE *pFILE = _tfdopen (fd, _T("a+"));
if(NULL == pFile)
{
CString strError(strerror(errno));
AfxMessageBox(strError);
}
In the OP attached image, pFILE is not NULL, meaning that _wfdopen succeeded.
It just points to a structure which has some poiner members NULL.
Re: Append Data to CFile via FILE*
Dear All,
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.