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

    Serialize an Array

    Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have.

    Thanks in advance.


  2. #2
    Join Date
    Apr 1999
    Posts
    90

    Re: Serialize an Array

    What are the element types and what array class are you using?



  3. #3
    Join Date
    Apr 1999
    Posts
    12

    Re: Serialize an Array

    You should use the MFC class 'CArray' This class has the member function 'serialize'. Any class that has the serialize member function can be save using the serialize mechanism. So define your array using the class CArray.


  4. #4
    Join Date
    May 1999
    Location
    St-Petersburg (Russia)
    Posts
    32

    Re: Serialize an Array

    for serializing:

    I usually write down the number of elements, then serialize each element..

    on deserializing:

    I read how many elements, then create that many elements and deserialize each of them.

    - Alex


  5. #5
    Join Date
    May 1999
    Posts
    28

    Re: Serialize an Array

    Using CPoint array of points for a drawing program so drawing can be saved when file is saved. Here is the code. Thanks for responding.<

  6. #6
    Join Date
    Apr 1999
    Posts
    90

    Re: Serialize an Array

    Sorry, I don't see any code. Unfortunately this discussion board doesn't allow attachments.

    For the MFC CArray template class, what I usually do it sub-class it using the element type that I plan on storing in the array and over-ride the operations that insert and remove elements from the array. That way I can keep track of the actual number of elements in the array rather than just what the upper bound is. You can also add Save & Load methods for reading and writing the elements of the array. The first thing that you'd need to write into the file would be the actual number of elements (points) that make up the array.

    If you need more help, you can reply.

    Michael




  7. #7
    Guest

    Re: Serialize an Array

    Have you checked out the Scribble tutorial that comes with Visual C++ v5.0 (I presume it's also part of v6.0)? It's great for learning how to write
    drawing code, and it includes a Serialize() routine.


  8. #8
    Join Date
    May 1999
    Posts
    28

    Re: Serialize an Array

    Hey, thanks for sticking with this! Let's try posting the code again:

    void CSimpleAppView::Serialize(CArchive& ar)
    {
    Serialize(ar);
    if (ar.IsStoring())
    { // storing code
    ar &lt;&lt; m_Color; //Store the pen color
    ar &lt;&lt; m_ptCount; //Store the drawing
    for (int i = 0; i &lt; m_ptCount; i++) //to be
    //retrieved
    {
    ar &lt;&lt; PointArray[i].x;
    ar &lt;&lt; PointArray[i].y;
    }
    }
    else
    { // loading code
    ar &gt;&gt; m_Color; //Retrieve the pen color
    ar &gt;&gt; m_ptCount; //Retrieve the previous
    for (int i = 0; i &lt; m_ptCount; i++) //drawing
    {
    ar &gt;&gt; PointArray[i].x;
    ar &gt;&gt; PointArray[i].y;
    }
    }
    }




  9. #9
    Join Date
    Apr 1999
    Posts
    90

    Re: Serialize an Array

    What exactly are you having trouble with?
    Did you look at the Scribble example?


  10. #10
    Join Date
    Apr 1999
    Posts
    90

    Re: Serialize an Array

    I don't see where you are subscripting the array to get to the elements.

    For example:
    ar &lt;&lt; Po

  11. #11
    Join Date
    May 1999
    Posts
    28

    Re: Serialize an Array

    Subscripting the array in the for loop:

    for (int i = 0; i &lt; m_ptCount; i++)
    {
    ar &lt;&lt; PointArray[i].x;
    ar &lt;&lt; PointArray[i].y;
    }

    However, for some reason when I post this to you, it drops the LEFT-SQUARE BRACKET i RIGHT-SQUARE BRACKET that I place after PointArray but before the .x

    The problem I am having is neither the pen color nor the drawing is saved when I save the file. As this is the first time I have used serialized, I've got no clue what else needs to be done.

    Do I need to do something in ON_SAVE_AS ?

    I am about to look into the Scribble sample. Thanks for suggesting it.


  12. #12
    Guest

    Re: Serialize an Array

    Any reason why you put Serialize() in your View class? It should go in
    your CSimpleAppDoc class.


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