|
-
April 30th, 1999, 11:49 AM
#1
Serialize an Array
Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have.
Thanks in advance.
-
April 30th, 1999, 02:50 PM
#2
Re: Serialize an Array
What are the element types and what array class are you using?
-
April 30th, 1999, 06:00 PM
#3
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.
-
April 30th, 1999, 11:14 PM
#4
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
-
May 3rd, 1999, 07:40 AM
#5
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.<
-
May 3rd, 1999, 08:15 AM
#6
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
-
May 3rd, 1999, 08:20 AM
#7
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.
-
May 3rd, 1999, 08:58 AM
#8
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 << 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;
}
}
}
-
May 3rd, 1999, 09:53 AM
#9
Re: Serialize an Array
What exactly are you having trouble with?
Did you look at the Scribble example?
-
May 3rd, 1999, 10:00 AM
#10
Re: Serialize an Array
I don't see where you are subscripting the array to get to the elements.
For example:
ar << Po
-
May 3rd, 1999, 10:15 AM
#11
Re: Serialize an Array
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.
-
May 3rd, 1999, 10:21 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|