c#/ c++ 读取二进制结构体数据

2 篇文章 0 订阅
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();



}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ListenAlone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值