Read last 6 bytes

rainbow-girl

Member
Joined
Jul 27, 2024
Messages
12
Programming Experience
Beginner
Hi

I try to read the LAST 6 bytes from a file. The output is 6 bytes which are 0.
But when I open the file with a hex editor, the last 6 bytes are not 0. What is wrong with my code?

This is the file:



code:
C#:
public void PrintByteArray(byte[] bytes)
{
    var sb = new StringBuilder("");
    foreach (var b in bytes)
    {
        sb.Append(b + ", ");
    }
    Console.WriteLine(sb.ToString());
}

FileStream filestream = File.Open("TL09.THF", FileMode.Open);

//laatste 6 bytes
filestream.Seek(6, SeekOrigin.End);
byte[] last6bytes = new byte[6];
filestream.Read(last6bytes, 0, 6);

PrintByteArray(last6bytes);
output:

0, 0, 0, 0, 0, 0,
 
Last edited by a moderator:
You should be doing all you can for yourself before asking us for help. How about you start by reading all the bytes of the file and compare them to what you see in the hex editor?
 
As an aside, if you took the return value of FileStream.Read(), it would have also told you how many bytes were actually read in. It would have very likely shown that no bytes were read. That would then have lead you to determine that you needed to set the read cursor 6 bytes before the the end of the stream instead of 6 bytes after the end of the stream.

Anyway which way, good job figuring it out by yourself!
 

Latest posts

Back
Top Bottom