D
D
Denis Goncharenko2016-12-01 13:15:09
Programming
Denis Goncharenko, 2016-12-01 13:15:09

Where do characters come from after saving a file?

There is an array of bytes, I output it to the crane, save it to a file via:

static void saveFile(string fileName, byte[] data)
        {
            FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
            fileStream.Write(data, 0, data.Length);
            fileStream.Close();
        }

I immediately open this file and read it into an array of bytes:
static byte[] readFile(string fileName)
        {
            FileStream fileStream = File.OpenRead(fileName);
            byte[] fileDataBytes = new byte[fileStream.Length];

            fileStream.Read(fileDataBytes, 0, fileDataBytes.Length);
            fileStream.Close();

            return fileDataBytes;
        }

And lo and behold, different byte arrays are obtained, that is, after opening the resulting array is longer and contains more additional characters.
I don’t know what the end-of-file character is added or what, but the bottom line is that I save the encoded file and in order to decode it later there should be no garbage, through what to save / read so that nothing is attributed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-12-01
@petermzg

By doing this:

FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
fileStream.Write(data, 0, data.Length);
fileStream.Close();

If you already have a file of 10 bytes, then writing 5 will give you a file size of 10 bytes, since the file is not recreated and the FileStream simply overwrites the values ​​from position 0.
This is where the extra bytes appear.

You write from byte 0 and FileStream does not give you any profit, just use System.IO.File, there should not be any problems, almost everything is set by default.
Can you post the output? Specifically: the input byte array, what did it store, and what did it read?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question