Solved

Serialization

Posted on 1998-11-17
5
280 Views
Last Modified: 2013-11-20
Here is what i am doing:
I need to work with intel hexa files. this files are composed by intel hexa lines ( each hexa line is a line in the file) and is terminated by a special line.
I have created one CIntelHexaLine object for treating lines and one CIntelHexaDoc for the file. My doc has one CtypedPtrArray of CIntelHexaLine.

The problem is serialization.
My intel line is saved as a CString in CIntelHexaLIne and i think i can not serialize CString objects?
Also for opening i am searching the end of a line. Isn´t there a way of reading files line by line?
Last question:
My serialization in the document is passing the work for the CIntelHexaLine Serialization. The termination condition (in serialization doc) is the line reading==end line. But if the file is corrupted and the end line never occurs? Does the >> operator triggers any exception if there is nothing else to read?

Thanks a lot for any comments/answers you can give me.
0
Comment
Question by:jmartins71
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:shaig
ID: 1324832
You can serialize a CString object to an archive.
Note that the actual storage file is in binary form!  That file contains alot of other data put by MFC else then the stuff you put in it.

The eziest way to implement what you want ( baut' that single line at a time thing ) is to inherite your class from CObject, and then hold all of your objects in a CObArray that serializes all of it's item automatically.

call the ObArray's Serialize from within the CDocument's Serialize.
0
 

Author Comment

by:jmartins71
ID: 1324833
Thanks shaig.
Although i select reject answer i am happy with your.
I just need some further explanations to close the question and give you the points.

Let me try to understand.

Here is my older doc serialize:
void CIntelHexaDoc::Serialize(CArchive& ar)
{
      if (ar.IsStoring())
      {
            int i = 0;
            while (i < m_HexaLinesArray.GetSize() )
            {    
                  m_HexaLinesArray.GetAt( i++ )
                        ->Serialize(ar);
            }

      }
      else
      {
            BOOL end;
            end = FALSE;
            while (!end)
            {
                  CIntelHexaLine * line = new CIntelHexaLine;
                  line->Serialize(ar);
                  if ( (line->strLine)==(line->strEndLine) )
                        end = TRUE;
                  else
                        Add(line);
            }
      }
}

what you say is:
void CIntelHexaDoc::Serialize(CArchive& ar)
{
      m_HexaLinesArray.Serialize( ar );
}
right?
m_HexaLinesArray is       
CTypedPtrArray<CObArray,CIntelHexaLine*>


But this may pose a problem to me.
As you say MFC puts things in the file. So if i send a saved intel hexa file to another device i am sending different things?
That is: If i load one intel hexa  (generated from other application) and then save it then the file will be different from the original? Oops.

By the way i have been investigating and i have discover ReadString and WriteString so my serialize intel hexa line is like this:
void CIntelHexaLine::Serialize( CArchive& ar )
{
      if( ar.IsStoring( ) )
      {      
            ar.WriteString (strLine);
      }
      else      
      {
            ar.ReadString (strLine);
      }
}

Do you aggree?
strLine is a CString.
With this i think i must create CArchive from one CStdioFile?
I am not using what the framework gives.
0
 
LVL 3

Accepted Solution

by:
shaig earned 40 total points
ID: 1324834
I didn't understand. Are you or are you not using the >> << operators? If you use operators that you must do it both way's, for reading and writing or use the Read and Write function, both ways to.

Note that CArchive code for the operators contains things like this:
CArchive& AFXAPI operator<<(CArchive& ar, const CString& string)
{
  // special signature to recognize unicode strings
  #ifdef _UNICODE
      ar << (BYTE)0xff;
      ar << (WORD)0xfffe;
  #endif
  ...
}

0
 

Author Comment

by:jmartins71
ID: 1324835
Let me give you further explanations.
I have one application that generates one intel hexa file from c code. this intel hexa file must be sended to a microcontroller. Him will then load one non volatil ram.

The application that i am developing must load intel hexa files to memory, work on them (like adding or substituing lines) and then sending the final result to the microprocessor. To send the intel hexa to the microprocessor a must use one protocol.

To solve this a "cry out" POLYMORFISM. that is a have one class CDS5002Prog derived from CFile. So my guess was using serialization with a CFile (to load the file) or with a CDS5002Prog.
But MFC saves lots of things like you say so this is a problem in my approach.
Also i must check if there is one E char when loading the intel hexa to the microprocessor ( the micro will send one E char if it detects one error).

I am using WriteString and not << (same for ReadString) because i think it is better to read lines (what i want is lines).

After a good night sleep i have decided to implement "my own serialization" (forgetting << >>).

Thanks Shaigs for your comments.
I will give you the points but i would like to hear from you some comments.
 
0
 
LVL 3

Expert Comment

by:shaig
ID: 1324836
Thanks.
I don't see nothing wrong with your decision.
The problam with microsoft's foundation is that when you want to do something that is not in the exact mainstream, you are on your own.
The MFC is not extensible enough. For example:
CArchive's Read() and Write() are not virtual!!

If you have any further problems, just leave a comment.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 102
Named range not carried over 10 57
if loop in java 3 114
fizzArray  challenge 1 47
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now