Doofus
April 30th, 1999, 11:49 AM
Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have.
Thanks in advance.
Thanks in advance.
|
Click to See Complete Forum and Search --> : Serialize an Array Doofus April 30th, 1999, 11:49 AM Trying to serialize an array without much luck. Would appreciate any suggestions you "grizzled veterans" might have. Thanks in advance. Michael Decker April 30th, 1999, 02:50 PM What are the element types and what array class are you using? Don Janik April 30th, 1999, 06:00 PM 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. Alex Black April 30th, 1999, 11:14 PM 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 Doofus May 3rd, 1999, 07:40 AM 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.< Michael Decker May 3rd, 1999, 08:15 AM 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 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. Doofus May 3rd, 1999, 08:58 AM 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; } } } Michael Decker May 3rd, 1999, 09:53 AM What exactly are you having trouble with? Did you look at the Scribble example? Michael Decker May 3rd, 1999, 10:00 AM I don't see where you are subscripting the array to get to the elements. For example: ar << Po Doofus May 3rd, 1999, 10:15 AM 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 Any reason why you put Serialize() in your View class? It should go in your CSimpleAppDoc class. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |