using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace FuntionTest
{
struct RulerPosition
{
public int rulerX;
public int rulerY;
public int w;
public int h;
}
class Program
{
private static RulerPosition Byte2Struct(byte[] arr)
{
int structSize = Marshal.SizeOf(typeof(RulerPosition));
IntPtr ptemp = Marshal.AllocHGlobal(structSize);
Marshal.Copy(arr, 0, ptemp, structSize);
RulerPosition rs = (RulerPosition)Marshal.PtrToStructure(ptemp, typeof(RulerPosition));
Marshal.FreeHGlobal(ptemp);
return rs;
}
static void Main(string[] args)
{
string path = @"E:\RedCellPostionInfo.bin";
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int structSize = Marshal.SizeOf(typeof(RulerPosition));
List<RulerPosition> list = new List<RulerPosition>();
byte[] bytes = new byte[structSize];
while (true)
{
int byteCounts = br.Read(bytes, 0, structSize);
if (byteCounts == 0) break;
RulerPosition pos = Byte2Struct(bytes);
list.Add(pos);
}
br.Close();
fs.Close();
for (int i = 0; i < list.Count; ++i)
{
System.Diagnostics.Debug.WriteLine($"x = {list[i].rulerX}" +
$" y = {list[i].rulerY} w = {list[i].w} h = {list[i].h} ");
}
}
}
}
#include <fstream>
#include <thread> // std::thread
#include <vector>
struct RulerPosition
{
unsigned int rulerX;
unsigned int rulerY;
unsigned int w;
unsigned int h;
};
int main()
{
std::vector<RulerPosition> m_positionsForRedCell;
for(int i = 0; i < 10; ++i)
{
RulerPosition p{i,i+1,i+2,i+3};
m_positionsForRedCell.push_back(p);
}
if (!m_positionsForRedCell.empty())
{
std::ofstream outRedCellPos("E:\\RedCellPostionInfo.bin", std::ios::binary|std::ios::out);
for each (auto pos in m_positionsForRedCell)
{
outRedCellPos.write(reinterpret_cast<char*>(&pos), sizeof(RulerPosition));
}
outRedCellPos.close();
}
m_positionsForRedCell.clear();
std::ifstream inRedCellPos("E:\\RedCellPostionInfo.bin", std::ios::binary | std::ios::in);
while(!inRedCellPos.eof())
{
RulerPosition pos;
inRedCellPos.read(reinterpret_cast<char*>(&pos), sizeof(RulerPosition));
if(!inRedCellPos.eof())
m_positionsForRedCell.push_back(pos);
}
inRedCellPos.close();
}