客户给的文件格式struct  SECTION 

char Type;   
double S3[3];   
}section;说明
Type分类
#define S_mileage          19
S3[0] 起始里程
S3[1] 结束里程
S3[2] 这个断面的S偏差
#define S_START           20
S3[0] 起始S
S3[1] 起始H
S3[2] 这个断面的H偏差
#define S_LINE           21
S3[0] 直线结束点的S
S3[1] 直线结束点的H
#define S_ARC1            22
S3[0] 圆心的S
S3[1] 圆心的H
S3[2] 弧的转角
请各位大虾帮忙给个方案啊,最好写个小例子,本人初学CS

解决方案 »

  1.   

            Dim file1 As FileStream = New FileStream(Z_Path & "DAY.dat", FileMode.Open)
            Dim myread As BinaryReader = New BinaryReader(file1)
            myread.BaseStream.Seek(65536, SeekOrigin.Begin)             myread.BaseStream.Seek(27, SeekOrigin.Current)
                NT.总天数 = myread.ReadInt32
                myread.BaseStream.Seek(4, SeekOrigin.Current) 
            myread.Close()
            file1.Close()
            file1.Dispose()用BinaryReader ,类似于上面这种代码,不好意思我是用vb.net的,你网上去找c#的BinaryReader代码,很多的
      

  2.   

    没看明白,你再写个实例.或读了保存为什么?一个string串?
      

  3.   


    最终的格式应当是起始里程 结束里程 这个断面的S偏差   12      22       33起始S   起始H   这个断面的H偏差 
      1       2         3大概就是这样的格式,问题是怎么去读?
      

  4.   

    感觉没有好的方法,直接用文件流来一行一行读,如果要做得好些,可以写个操作类,实现得到某个节点(我理解:一个#define就是一个节点)下某个值(从你的文件看是3个,S[0]~S[2])等的操作.
      

  5.   

    现将文件里的数据读到和结构一样大小的缓存(buffer)中;
    获取这段缓存的内存首地址UnsafeAddrOfPinnedArrayElement;
    将结构对象的地址指向该地址即可。参考如下代码:
    //using System.Media;
    //using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct SECTION
    {
        public byte Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
        public double[] S3;
    }private void button1_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(@"c:\temp\temp.dat", 
            FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[Marshal.SizeOf(typeof(SECTION))];
        fileStream.Read(buffer, 0, buffer.Length);
        SECTION section = (SECTION)Marshal.PtrToStructure(
            Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0), typeof(SECTION));
        fileStream.Close();
        fileStream.Dispose();    Console.WriteLine("Type={0}, S3[0]={1}, S3[1]={2}, S3[2]={3}", 
            section.Type, section.S3[0], section.S3[1], section.S3[2]);
    }
      

  6.   

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
    这个大小是测试用的,实际是元素个数,这里是3.
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
      

  7.   

    得出来的数据成这样了
    Type=19, S3[0]=4.10568551694076E-321, S3[1]=0, S3[2]=-2.10648588040163E-37以上除了type的值是对了其它的都是有问题的
      

  8.   

    http://www.brsbox.com/filebox/down/fc/4c6031fe8e287fec08bb782599c8afdf我传了其中的一个文件到网盘中,
    格式是这样的
    typedef struct{
    char Type;   
    double D5[5];   
    }Halign;
    说明
    Halign[0].Type
    ==1  代表start
    #define START           1
    Halign[0].D5[0]
    起始里程
    Halign[0].D5[1]
    起始N
    Halign[0].D5[2]
    起始E
    Halign[0].D5[3]
    起始方位角
    Halign[1]以及以后的结构体根据Type判断类型
    类型分类如下
    #define LINE            2
    D5[0]直线长度
    #define ARC             3
    D5[0]圆弧半径
    D5[1]圆弧弧长
    #define SPIRAL          4
    D5[0]缓和曲线参数
    D5[1]缓和曲线起始半径
    D5[2]缓和曲线结束半径请帮我看看应当怎么做,谢谢
      

  9.   

    这个文件打开分析了下,有五段数据,每段44字节:1(char)+5*8(double)=41
    看来多3个字节,第一char对齐估计是4.[StructLayout(LayoutKind.Sequential, Pack = 4)]
    public class SECTION
    {
        public byte Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
        public double[] D5 = new double[5];
    }private void button2_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(@"c:\temp\123.HAL",
            FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[Marshal.SizeOf(typeof(SECTION))];
        while (fileStream.Read(buffer, 0, buffer.Length) >= buffer.Length)
        {
            SECTION section = new SECTION();
            Marshal.PtrToStructure(
                Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0), section);        Console.WriteLine(
                "Type={0}, D5[0]={1}, D5[1]={2}, D5[2]={3}, D5[3]={4}, D5[4]={5}",
                section.Type, section.D5[0], section.D5[1], section.D5[2],
                section.D5[3], section.D5[4]);
        }
        fileStream.Close();
        fileStream.Dispose();
    }
      

  10.   

    哦,我明白了,是一个double占8个字节以此来推算的吧
    贴子加分20谢谢大家的回答,顺便再问一下
    我要是修改了其中的数据比如:D5[0]=100 修改成 D5[0]=200 想要保存进去应该怎么做fileStream.Write能行吗,怎么样才能写到那个位置
      

  11.   

    写入参考如下代码:
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public class SECTION
    {
        public byte Type;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
        public double[] D5 = new double[5];
    }private void button1_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(@"c:\temp\temp.dat",
            FileMode.Create, FileAccess.Write);
        byte[] buffer = new byte[Marshal.SizeOf(typeof(SECTION))];
        
        SECTION section = new SECTION();
        section.Type = 13;
        section.D5[0] = 12.34;
        section.D5[1] = 56.78;
        section.D5[2] = 90.12;
        section.D5[3] = 34.56;
        section.D5[4] = 78.90;
        Marshal.StructureToPtr(section, 
            Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0), false);    fileStream.Write(buffer, 0, buffer.Length);
        // ...写入多条数据,就重复上面的执行。
        fileStream.Close();
        fileStream.Dispose();
    }