Click to See Complete Forum and Search --> : urgent !! serialization-problems by loadind array


Rene Greulich
March 30th, 1999, 12:45 PM
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();

}

}

}

Dave Lorde
March 31st, 1999, 07:25 AM
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