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

    How to archive a CStringArray

    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


  2. #2
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: How to archive a CStringArray

    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

  3. #3
    Join Date
    Apr 1999
    Posts
    383

    Re: How to archive a CStringArray

    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



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