使用C#将结构中的字节写入文件 [英] Writing bytes from a struct into a file with c#

查看:115
本文介绍了使用C#将结构中的字节写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好!

我在这里遇到一个新问题。我必须写下一个数据,该数据来自我在系统中声明的结构。

I'm with a new problem here. I have to write down a data that comes from a struct that I declared in my sistem.

我创建的结构只有两个字段,以后我会用它来构造它

The struct I created has only two fields and I use to to later conevrt it into bytes.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct MyStructData
{
    public short Id;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string Name;
}

我用以下代码将该结构转换为字节:

I convert this struct in bytes with the following code:

private byte[] getBytes(MyStructData aux)
{
    int length = Marshal.SizeOf(aux);
    IntPtr ptr = Marshal.AllocHGlobal(length);
    byte[] myBuffer = new byte[length];

    Marshal.StructureToPtr(aux, ptr, true);
    Marshal.Copy(ptr, myBuffer, 0, length);
    Marshal.FreeHGlobal(ptr);

    return myBuffer;
}

此函数用于转换MyStructData类型的List结构中的每个元素我拥有要发送到另一台机器的所有寄存器的元素,并且使用下面粘贴的代码来做到这一点:

This function is made to transform each element inside in a List structure of MyStructData type elements where I have all the registers I want to send to another machine, and I do it with the code pasted below:

string saveToFilePath = "D:\\" + filename;
Stream myStream = myFtpRequest.GetRequestStream();

using (FileStream myFileStream = new FileStream(saveToFilePath, FileMode.Create))
{
    foreach (MyStructData myData in myStructDataList)
    {
        int length = 2048;
        byte[] newBuffer = new byte[length];
        newBuffer = getBytes(myCust);

        int len = 0;
        int bytesRead = 0;

        myFileStream.Write(newBuffer, 0, len);
        bytesRead += len;
    }

    myFileStream.Close();
}

我的问题是我看到我的新文件为空,我可以看不到为什么它没有得到我的字节信息。我已经检查了列表是否包含数据,并且还检查了字节转换功能是否也可以正常工作,但是我无法直截了当地查看导致文件为空的原因。

My problem comes that I see that my new file is empty and I can't see why it doesn't gets the information of my bytes. I already checked if the List comes with data or not and I also checked that my byte convertion function works also perfectly, but I can't get to the point to see what's causing that my file is empty.

如果有人知道隧道尽头的灯光,我将不胜感激!

If someone knows the light at the end of the tunnel I would appreciate a lot your help!

编辑现在我有了另一种将数据写入文件的方法,并且效果很好:

EDIT Now I have another method to write the data into the file and it works good:

using (Stream stream = new FileStream(saveToFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
    using (BinaryWriter writer = new BinaryWriter(stream, Encoding.Default))
    {
        // get all data
        foreach (MyStructData myData in myStructDataList)
        {
            byte[] newBuffer = getBytes(cd);
            writer.Write(newBuffer);
        }
    }
}


推荐答案

FileStream.Write 的第三个参数是要写入的字节数。

The third parameter to FileStream.Write is the count of bytes to write. It should not be 0.

string saveToFilePath = "D:\\" + filename;
Stream myStream = myFtpRequest.GetRequestStream();

int bytesWritten = 0;
using (FileStream myFileStream = new FileStream(saveToFilePath, FileMode.Create))
{
    foreach (MyStructData myData in myStructDataList)
    {
        newBuffer = getBytes(myData);
        myFileStream.Write(newBuffer, 0, newBuffer.Length);
        bytesWritten += newBuffer.Length;
    }
}

这篇关于使用C#将结构中的字节写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆