Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have.
Thanks in advance.
Printable View
Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have.
Thanks in advance.
What are the element types and what array class are you using?
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.
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
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.<
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
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.
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 << m_Color; //Store the pen color
ar << m_ptCount; //Store the drawing
for (int i = 0; i < m_ptCount; i++) //to be
//retrieved
{
ar << PointArray[i].x;
ar << PointArray[i].y;
}
}
else
{ // loading code
ar >> m_Color; //Retrieve the pen color
ar >> m_ptCount; //Retrieve the previous
for (int i = 0; i < m_ptCount; i++) //drawing
{
ar >> PointArray[i].x;
ar >> PointArray[i].y;
}
}
}
What exactly are you having trouble with?
Did you look at the Scribble example?
I don't see where you are subscripting the array to get to the elements.
For example:
ar << Po
Subscripting the array in the for loop:
for (int i = 0; i < m_ptCount; i++)
{
ar << PointArray[i].x;
ar << 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.
Any reason why you put Serialize() in your View class? It should go in
your CSimpleAppDoc class.