Why can't I append a new byte array to a file using FileStream?

I am trying to write a byte array to a file, multiple times. The FileMode is set to Append, to create the file if it doesn't exist, otherwise open the file and seek to the end of the file as described. The problem is that when writing to the existing file, it gets overwritten rather than have the new byte array appended to it. That's all there is to it.

void WriteToFile()
{
    byte[] buffer = new byte[16 * 1024];
    int num;

    using (FileStream dest_stream = new FileStream(filename, FileMode.Append, FileAccess.Write))
    {
        while (num = ReadFromAnotherStream(my_other_stream, ref buffer) > 0)
            dest_stream.Write(buffer, 0, num);
    }
}

This function will be called occasionally. If the file already exists, seek to the end of the file and continue writing from there, otherwise create a new file and write data.


When it should append, it overwrites... It does not append.

It should append to the file instead of overwrite it.

There is no error thrown.

Using Seek for the FileStream does nothing.

When it overwrites, the data is correct, however, it needs to be appended at the end of the previous data, and not overwrite.



from Recent Questions - Stack Overflow https://ift.tt/3p0Zovu
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)