Append Data to CFile via FILE*
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Append Data to CFile via FILE*

  1. #1
    Join Date
    Dec 2004
    Posts
    61

    Unhappy 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.
    Attached Images Attached Images  

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    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.?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,390

    Re: Append Data to CFile via FILE*

    Quote Originally Posted by VbEndUser View Post
    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.
    Victor Nijegorodov

  4. #4
    Join Date
    Dec 2004
    Posts
    61

    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.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,090

    Re: Append Data to CFile via FILE*

    Quote Originally Posted by VbEndUser View Post
    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.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    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.
    Last edited by ovidiucucu; September 28th, 2012 at 04:56 PM. Reason: typos
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  8. #8
    Join Date
    Dec 2004
    Posts
    61

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center