Click to See Complete Forum and Search --> : How to archive a CStringArray


Will Rothwell
April 30th, 1999, 02:11 AM
Hi,

Can any one tell me how to achive a CStringArray object to a file. MFC help says CStringArray can be serialised. I tried and could not compile. I have the following code fragment:
CString strFileName = strEnvFullPath + "\\DATA.TMP";
CStringArray straUserList;
straUserList.SetSize(1);
int nListCount = 0;
CFile file(strFileName, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file, CArchive::store);
ar << straUserList; /*This is the ERROR line */

The error message I got is :
C:\DDK\src\ThSS\APP\SCR\SCREENER.CPP(1575) : error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class CArchive' (or there is no acceptable conversion)

On retrieving I put ar >> straUserList; , and I got:
C:\DDK\src\ThSS\APP\SCR\SCREENER.CPP(1978) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class CStringArray' (or there is no acceptable conversion)

I cannot use the supplied Serialize(..) function because I am stuffing objects to a file run time. However,
if I use a for loop doing something like this is OK:
for (int i = 0; i < nCount<i++) ar << straUserList[i];

Any idea please?

Will

Troy T
April 30th, 1999, 02:21 AM
Instead of actually archiving the CStringArray, why not actually write the encapsulated strings (that reside in the CStringArray) to the file? Like so:

for( int nIndex = 0; nIndex < straUserList.GetCount(); nIndex++ )
{
file.Write( straUserList.GetAt(nIndex) );
}


That should do the trick. Instead of trying to have the serialization routines actually grab the data from the CStringArray pull the data from that array yourself, and write those strings to the file manually. This should yield more control to you, and let you format the data however you'd like. :) Good luck with it!



- Troy

Dave Lorde
April 30th, 1999, 04:18 AM
CStringArray::Serialize(CArchive& ar) is the function you must call. The streaming operators << and >> are generally only defined for basic types.

> I cannot use the supplied Serialize(..) function because I am stuffing
> objects to a file run time

I don't understand; why does "stuffing objects to a file run time" (whatever that means) stop you using Serialize?

CString strFileName = strEnvFullPath + "\\DATA.TMP";
CStringArray straUserList;
straUserList.SetSize(1);
int nListCount = 0;
CFile file(strFileName, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file, CArchive::store);
...
straUserList.Serialize(ar); // What's stopping you using this?

Dave