Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using VS2008 MFC project. I have the following code:
TCHAR* pszFileName = _T("c:\\ProjectOne Code\\ProjectOne\\ProjectOne\\test.txt");
CFile f;
f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&f, CArchive::store);
for(x=0; x<7; x++)
{
    numarray[x][0] = m_ListCtrl.GetItemText(x,0);
    ar << numarray[x][0];
    numarray[x][1] = m_ListCtrl.GetItemText(x,1);
    ar << numarray[x][1];
}
ar.Close();
f.Close();

The code writes all the information to my text file as expected. I then want to read the file contents back in to confirm data is stored correctly.

If I the try to do this: CArchive ar(&amp;f, CArchive::load); in the same function, I get an error stating I am trying to redefine ar. Is there a way to delete the ar object so I can recreate it for loading? I know if I leave the function the object is destroyed automatically. But there should be a way to destroy it in the function so the same object name can be reused.

If I use the following code in the function:
CArchive br(&f, CArchive::load);
f.Open(pszFileName, CFile::modeRead);
for(x=0; x<7; x++)
{
    numarray[x][0] = "";
    numarray[x][1] = "";
}
for(x=0; x<7; x++)
{
    br >> numarray[x][0];
    br >> numarray[x][1];
    MessageBox(numarray[x][0], numarray[x][1]);
}
br.Close();
f.Close();

I am able to read back the information from the file and confirm it is correct. There is no real issue with using a different object but I am just curious as to why I can't delete the original ar object and re-use it.
Posted
Comments
Richard MacCutchan 22-Nov-10 15:02pm    
*ar << ...
You need to apply the operator to the actual object, not to the pointer.
Richard MacCutchan 22-Nov-10 16:24pm    
I'm glad my suggestions helped you, even if you choose an alternative solution. I think you can also use some of the CArchive load and store functions directly; i.e. you are not forced to use the insertion (<<) and extraction (>>) operators for every case.

In your first function your archive object ar is instantiated within the function, so it will only be destroyed when it goes out of scope, i.e. when you exit the function. As you have seem you cannot redeclare the same object to be something different from its first declaration. If you wish to reuse it then make ar a pointer thus: CArchive* ar, and use new and delete to manage it.
 
Share this answer
 
Comments
Dalek Dave 22-Nov-10 5:08am    
Good Answer, Richard.
2buck56 22-Nov-10 12:37pm    
Richard, thanks for the suggestion. However, I've tried using CArchive* ar with new and delete. Even after you delete the ar pointer, you cannot re-use it. The compiler complains with the same error message. If I am going to have to create 2 objects anyway, I might as well use ar for writing and br for reading.
2buck56 wrote:
Richard, thanks for the suggestion. However, I've tried using CArchive* ar with new and delete. Even after you delete the ar pointer, you cannot re-use it. The compiler complains with the same error message. If I am going to have to create 2 objects anyway, I might as well use ar for writing and br for reading. -


The compiler will not complain if you reuse an object following a delete, perhaps you are trying to redeclare it thus:
CArchive* ar = new Carchive(&f, CArchive::store);
...
delete ar;
CArchive* ar = new Carchive(&f, CArchive::load);
...

whereas your second statement should be written as
ar = new Carchive(&f, CArchive::load);

You have already declared ar as a pointer to a CArchive object so you must not redeclare it.
 
Share this answer
 
Comments
2buck56 22-Nov-10 14:56pm    
Hmmm..... This code:
CArchive* ar = new CArchive(&f, CArchive::store);
for(x=0; x<7; x++)
{
numarray[x][0] = m_ListCtrl.GetItemText(x,0);
ar << numarray[x][0];
numarray[x][1] = m_ListCtrl.GetItemText(x,1);
ar << numarray[x][1];
}
The compiler gives this error:
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'CArchive *' (or there is no acceptable conversion)
2buck56 22-Nov-10 15:51pm    
Richard, I thought I had tried *ar << but I must not have because that does work. This was just a learning experience as I don't think there is any real advantage to using *ar versus using ar and br for store and load operations. But it does give me knowledge for future use. Thanks for all your help.
You could reuse a function as well :) :
class CYourArray
{
...
  static LPCTSTR sm_lpszDefFileName;
...
public:
...
  bool ExchangeData(bool bLoad, LPCTSTR lpszFileName = sm_lpszDefFileName);
};
 
Share this answer
 
v3
Comments
Richard MacCutchan 22-Nov-10 15:06pm    
Are you sure you meant to post this answer in this thread? I see nothing here about re-using a CArchive object.
Eugen Podsypalnikov 22-Nov-10 16:43pm    
The both sequences below are re-using the constructor code only not an object :) :
{
CArchive* pA(new CArchive(&f, CArchive::store));
...
delete pA;
pA = new CArchive(&f, CArchive::load);
...
delete pA;
}

bool CYourArray::ExchangeData(bool bLoad, ...)
{
...
CArchive ar(&f, bLoad ? CArchive::load : CArchive::store);
...
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900