file pointer reset of CArchive

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leejwen
    New Member
    • Jun 2007
    • 50

    file pointer reset of CArchive

    Hello, I used below MFC code to read a file, but I need do that several times, how can I move the pointer to the beginning of file once again. Thank you!

    CFile f;
    CArchive ar(&f, CArchive::load) ;
    ...
    while(ar.ReadSt ring(ptr))
    {
    ...
    }

    //here should reset to the beginning of file

    //re-read the file again
    while(ar.ReadSt ring(ptr))
    {
    ...
    }
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    See CFile reference, in particular take a look at the various functions affecting the position within the file.

    Comment

    • leejwen
      New Member
      • Jun 2007
      • 50

      #3
      Originally posted by arnaudk
      See CFile reference, in particular take a look at the various functions affecting the position within the file.
      Thank you! That's helpful

      Comment

      • leejwen
        New Member
        • Jun 2007
        • 50

        #4
        Originally posted by leejwen
        Thank you! That's helpful
        But why second "while" doesnt work? see below. (It seems the pointer is still at the end of file)

        CFile f;
        CArchive ar(&f, CArchive::load) ;
        ...
        while(ar.ReadSt ring(ptr))
        {
        //work well
        }

        ar.Flush();
        ar.GetFile()->SeekToBegin( );
        ar.Flush();
        while(ar.ReadSt ring(ptr))
        {
        //
        }

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          The thing is you're reading from a CArchive, not the file directly. Try to close and reopen the CArchive it before the second loop. Check the CArchive reference.

          Comment

          • leejwen
            New Member
            • Jun 2007
            • 50

            #6
            play a small trick to finish that,

            CFile f;
            {
            CArchive ar(&f, CArchive::load) ;
            while(ar.ReadSt ring())
            {
            //
            }
            }
            f.SeekToBegin() ;
            {
            CArchive ar(&f, CArchive::load) ;
            while(ar.ReadSt ring())
            {
            //
            }
            }

            Thank you!

            Comment

            Working...