[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Saving List Items to Disk

I have the following code in my project:

void CIRCServerConnector::SaveServerList(void)
{
      CFile fileServLst("servlist.dat", CFile::modeCreate | CFile::modeReadWrite);

      CArchive arStore(&fileServLst, CArchive::store);

      POSITION aPos = m_listServers.GetHeadPosition();
      CIRCServer* temp;

      while(aPos)
      {
            temp = m_listServers.GetNext(aPos);
            arStore.WriteObject(temp);
      }
      arStore.Close();
}

It is supposed to go through a CTypedPtrList and save each item to disk..one by one. Right now...I am getting a dialog box that pops up and says "An unsupported operation was attempted"...never seen that before. Can anyone help tell me what is going on here?
0
SGyves
Asked:
SGyves
  • 5
  • 2
1 Solution
 
Roshan DavisCommented:
Attempting to retrieve a CIRCServer* on any other object will result in a system error (the utterly enigmatic "An unsupported operation was attempted"), MSDN documented something like this.

can u tell me, in which point of code that error comes..?

Rosh :)
0
 
PriyeshCommented:
Just a reminder list, you might be already doing this.

class CIRCServer : public CObject//derive from cobject to
{
  DECLARE_SERIAL(CIRCServer)
public:
   void Serialize(CArchive& ar) ;
public:
   CString szName ;
};

IMPLEMENT_SERIAL(CIRCServer, CObject, 1)//needed to enable serialization

void CIRCServer::Serialize(CArchive& ar)//Called by WriteObject
{
   if(ar.IsLoading())
     ar >> szName ;
   else
     ar << szName ;  
}

And, a try - catch to catch CArchiveException or others to get any further details abt the error
0
 
SGyvesAuthor Commented:
Rosh...it happens right on the WriteObject line:

arStore.WriteObject(temp);


that one.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SGyvesAuthor Commented:
OMG...I think I forgot the IMPLEMENT_SERIAL and DECLARE_SERIAL statements.   (Slapping my head)

0
 
SGyvesAuthor Commented:
A long day indeed...that was it.
0
 
SGyvesAuthor Commented:
Hey...what is the best way to read these back from disk untill there are no more....meaning...how can I check for EOF??
0
 
PriyeshCommented:
Would this work for you??

      CIRCServer clsServer ;
      clsServer.szName = "IRCServer" ;

      CTypedPtrArray<CObArray, CIRCServer*> clsArraySave, clsArrayLoad ;
      clsArraySave.Add(&clsServer) ;

//Write.
      {
            CFile clsFile("c:\\ircobjects.dat", CFile::modeCreate|CFile::modeWrite) ;
            CArchive ar(&clsFile, CArchive::store) ;
            clsArraySave.Serialize(ar) ;
      }


//Read
      {
            CFile clsFile("c:\\ircobjects.dat", CFile::modeRead) ;
            CArchive ar(&clsFile, CArchive::load) ;
            clsArrayLoad.Serialize(ar) ;

            CIRCServer* pServer = clsArrayLoad.GetAt(0) ;
            TRACE("Name = %s\n", pServer->szName) ;
      }

0
 
SGyvesAuthor Commented:
Yes...I believe that will work. As I understand...the serialize for the CTypedPtrList serializes all elemnts into the file. Cool.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now