Search code examples
c#structbytefilestream

Writing bytes from a struct into a file with c#


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

Solution

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