Writing bytes from a struct into a file with c#

2019-05-29 05:55发布

问题:

Good morning everyone!

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;
}

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);
        }
    }
}

回答1:

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;
    }
}


回答2:

I don't think you should be writing the data to a file like this. For a simple struct like that, you would better off using a BinaryFormatter.

Here's an example. First you need to make MyStructData serializable by adding the [Serializable] attribute:

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

Then you can write a list of those structs to a file like so:

List<MyStructData> data = new List<MyStructData>();

for (short i = 0; i < 100; ++i)
    data.Add(new MyStructData{Id = i, Name = i.ToString()});

string filename = "C:\\test\\test.data";

using (var file = File.OpenWrite(filename))
{
    var writer = new BinaryFormatter();
    writer.Serialize(file, data); // Writes the entire list.
}

And you can read them back like so:

using (var file = File.OpenRead(filename))
{
    var reader = new BinaryFormatter();
    data = (List<MyStructData>) reader.Deserialize(file); // Reads the entire list.
}

foreach (var item in data)
    Console.WriteLine("Id = {0}, Name = {1}", item.Id, item.Name);

If the only reason you added the marshalling to the struct was so you can write and read it to and from a file, you could then remove it so your struct would look like this:

[Serializable]
public struct MyStructData
{
    public short Id;
    public string Name;
}