Solved

Serialization

Posted on 1998-11-17
5
287 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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