CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    22

    urgent !! serialization-problems by loadind array



    Hi

    I am trying to load from a file, but do not know when it is the EOF.

    Therefore I use a while in my serialization to do the serialization until it is the end of the file. But with my code

    the while is done only once.

    Hopefully you can help me.

    void Telefon::load()

    {

    CFile Telefonbuch;

    if(!Telefonbuch.Open("C:\\tel\\tel.sv", CFile::modeCreate |

    CFile::modeNoTruncate |

    CFile::modeReadWrite))

    {

    }

    CArchive ar(&Telefonbuch, CArchive::load);

    Serialize(ar);

    ar.Close();

    Telefonbuch.Close();

    }

    void Telefon::Serialize(CArchive &ar)

    {

    int i = 0;

    CObject::Serialize(ar);

    if(ar.IsStoring())

    {

    while(i < iterator_)

    {

    ar << tbuch[i].name_ ;

    ar << tbuch[i].vorname_;

    ar << tbuch[i].str_;

    ar << tbuch[i].plz_;

    ar << tbuch[i].nummer_;

    ar << tbuch[i].fax_;

    ar << tbuch[i].email_;

    ar << tbuch[i].firma_;

    ar << tbuch[i].handy_;

    ar << tbuch[i].home_;

    i++;

    }

    }

    else

    {

    CString name, vorname, str, plz, nummer, fax, email,

    firma,handy, home;

    CFile *pFile = ar.GetFile();

    //LONG lOff = 0;

    //DWORD endpos = pFile->Seek(0L, CFile::end);

    pFile->SeekToBegin();

    //DWORD pos = pFile->GetPosition();//Seek(0L,

    //pFile- >begin);

    while(!pFile->GetPosition())

    {

    ar >> name;

    ar >> vorname;

    ar >> str;

    ar >> plz;

    ar >> nummer;

    ar >> fax;

    ar >> email;

    ar >> firma;

    ar >> handy;

    ar >> home;

    enter(name, vorname, str, plz, nummer, fax, email,

    firma,handy, home);

    // pos = pFile->GetPosition();

    }

    }

    }



  2. #2
    Join Date
    Apr 1999
    Posts
    383

    Re: urgent !! serialization-problems by loadind array



    CFile::GetPosition() returns the position of the file pointer. It is only zero at the beginning of the file. Your while loop is checking whether it is zero, so it only works the first time through. Next time round, the pointer is halfway down the file, so the while loop test fails.


    You could compare the position returned by GetPosition with the file size returned by SeekToEnd:


    DWORD fileEnd = pFile->SeekToEnd();

    pFile->SeekToBegin();


    ...


    while(pFile->GetPosition() < fileEnd)

    {

    ...

    }


    Dave

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured