The serialization of instance parsing C++/CLI

Source: Internet
Author: User
Tags bool datetime serialization

Serialization enables an object to be converted to an external form, such as file storage for use in a program, or through communication between programs to another process. The process of converting to an external form is called "serialization," and the inverse process is called "drag".

Brief introduction

Consider the example in Example 1, which writes the values of multiple object types to a new disk file, closes the file, and then reads the values back into memory.

Example 1:

using namespace System;
using namespace System::IO;
using namespace System::runtime::serialization::formatters::binary;
int main ()
{
array<int>^ Intarray = {10, 20, 30};
array<float,2>^ Floatarray = {
{1.2F, 2.4F},
{3.5F, 6.8F},
{8.4F, 9.7F}
};
DateTime dt = Datetime::now;
Console::WriteLine ("dt >{0}<", DT);
/*1*/binaryformatter^ formatter = gcnew BinaryFormatter;
Serializing data to a file
/*2*/stream^ file = File::open ("Sr01.ser", filemode::create);
/*3a*/formatter->serialize (file, "Hello");
/*3b*/formatter->serialize (file, intarray);
/*3c*/formatter->serialize (file, Floatarray);
/*3d*/formatter->serialize (file, true);
/*3e*/formatter->serialize (file, DT);
/*3f*/formatter->serialize (file, 1000);
/*3g*/formatter->serialize (file, L ' X ');
/*3h*/formatter->serialize (file, 1.23456F);
/*4*/File->close ();
Drag rows of data from a file--that is, reading data
/*5*/file = File::open ("Sr01.ser", Filemode::open);
/*6a*/string^ s = static_cast<string^> (formatter->deserialize (file));
Console::WriteLine ("String >{0}<", s);
/*6b*/array<int>^ Newintarray =
Static_cast<array<int>^> (formatter->deserialize (file));
Console::WriteLine ("Newintarray:");
for (int i = 0; i < newintarray->length; ++i)
{
Console::write ("{0}", Newintarray[i]);
}
Console::WriteLine ();
/*6c*/array<float,2>^ Newfloatarray =
Static_cast<array<float,2>^> (formatter->deserialize (file));
Console::WriteLine ("Newfloatarray:");
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 2; ++j)
{
Console::write ("{0}", Newfloatarray[i,j]);
}
Console::WriteLine ();
}
/*6d*/bool B = static_cast<bool> (formatter->deserialize (file));
Console::WriteLine ("bool >{0}<", b);
/*6e*/DateTime NEWDT = static_cast<datetime> (formatter->deserialize (file));
Console::WriteLine ("Newdt >{0}<", NEWDT);
/*6f*/int v = static_cast<int> (formatter->deserialize (file));
Console::WriteLine ("int >{0}<", V);
/*6g*/wchar_t C = static_cast<wchar_t> (formatter->deserialize (file));
Console::WriteLine ("wchar_t >{0}<", c);
/*6h*/float f = static_cast<float> (formatter->deserialize (file));
Console::WriteLine ("Float >{0}<", f);
/*7*/File->close ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.