writing a byte array from as string using an unkno

2019-08-30 07:21发布

问题:

i have the following code:

    int BufSize = 60000000;
    int BufSizeM1M = BufSize - 1000000;
    byte[] ByteBuf = new byte[BufSizeM1M];
    byte[] ByteBufVer = new byte[BufSizeM1M];
    using (WinFileIO WFIO = new WinFileIO(ByteBuf))
    {
        WFIO.OpenForWriting(path);
        Byte[] BytesInFiles = GetBytes(content);
        WFIO.WriteBlocks(BytesInFiles.Length);
    }

EDIT: This is the original code i was working with, trying to modify it myself seems to fail, so i was thinking you guyz might have a look:

  int BufSize = 60000000;
  int BufSizeM1M = BufSize - 1000000;
  byte[] ByteBuf = new byte[BufSizeM1M];
  byte[] ByteBufVer = new byte[BufSizeM1M];
  int[] BytesInFiles = new int[3]
  using (WinFileIO WFIO = new WinFileIO(ByteBuf))
      WFIO.OpenForWriting(path);
      WFIO.WriteBlocks(BytesInFiles[FileLoop]);
  }

FileLoop is an int between 0-3 (the code was run in a loop) this was used for testing write speed. how would one change it to write actual content of a string?

the WFIO dll was provided to me without instructions and i cannot seem to get it to work. the code above is the best i could get, but it writes a file filled with spaces instead of the actual string in the content variable. help please.

回答1:

I think you might be missing a step here. Once you've done:

Byte[] BytesInFiles = GetBytes(content); 

Won't you need to do something with BytesInFiles? Currently it seems as though you are writing chunks of BytesInFiles, which will have been initialized to contain all zeros when you created it.

Edit: Would something like this help?

Byte[] BytesInFiles = GetBytes(content); 

using (WinFileIO WFIO = new WinFileIO(BytesInFiles)) 
{ 
    WFIO.OpenForWriting(path);        
    WFIO.WriteBlocks(BytesInFiles.Length); 
} 


回答2:

You seem to be passing only a length (number of bytes) to this components so it probably doesn't know what to write. Your ByteBuf array is initialized to an empty byte array and you probably write out BytesInFiles.Length number of 0-s. You are putting the converted content into BytesInFiles but you never use that buffer for writing - you only use its length.



标签: c# dll file-io