IOLink C# 1.11.0
Loading...
Searching...
No Matches
SerializeArray.cs

This code demonstrates how to serialize and deserialize an ArrayXf object to/from a file. The code creates a 2D array of size 4x2, fills it with float values and writes it to a file. It then reads the data back and prints the content of the array.

using IOLink;
using System;
namespace Examples
{
public class SerializeArray
{
static void SerializeArrayXf()
{
// path to a temporary file in the temp directory
string tmpFilePath = System.IO.Path.GetTempPath() + "serialization_array_net.txt";
VectorXu64 shape = new VectorXu64(4, 2);
ArrayXf arrayf = new ArrayXf(shape);
// fill the array with some values, cell by cell
arrayf.SetAt(new VectorXu64(0, 0), 0.0F);
arrayf.SetAt(new VectorXu64(1, 0), 1.0F);
arrayf.SetAt(new VectorXu64(2, 0), 2.0F);
arrayf.SetAt(new VectorXu64(3, 0), 3.0F);
arrayf.SetAt(new VectorXu64(0, 1), 10.0F);
arrayf.SetAt(new VectorXu64(1, 1), 11.0F);
arrayf.SetAt(new VectorXu64(2, 1), 12.0F);
arrayf.SetAt(new VectorXu64(3, 1), 13.0F);
Console.WriteLine("Serialize array into " + tmpFilePath);
// open a streamAccess to serialize the array to a
// file
if (dst == null)
{
throw new Exception("Impossible to open file " + tmpFilePath);
}
// array is serialized to the file
Serialization.EncodeArrayXf(arrayf, dst);
// flush and close the streamAccess
dst.Flush();
dst = null;
// open another streamAccess to read the serialized
// array from the file
if (src == null)
{
throw new Exception("Impossible to open file " + tmpFilePath);
}
// array is deserialized from the file
ArrayXf arrayRead = Serialization.DecodeArrayXf(src);
// print the content of the array
Console.WriteLine("Content of unserialized array:" + arrayRead.ToString());
}
public static void Main(string[] args)
{
SerializeArrayXf();
Console.WriteLine("SUCCESS");
}
}
}
Definition CreateDataFrame.cs:6