Avatar of XK8ER
XK8ERFlag for United States of America asked on

vb.net - peek or read

Hello,
how can I know the last HEX byte of a 2GB binary file without opening the whole file..
is there a easy way for doing this without running into memory problems?
Visual Basic.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
Robert Schutt

8/22/2022 - Mon
AndyAinscow

As far as I know you will have to open the complete file and move (read) to the end - which you could do in smaller chuncks eg with a 1MB buffer you read the file into in a loop until you get the final section.
ASKER
XK8ER

is there a faster way to just read the last bytes?
AndyAinscow

I can think of a couple.
Use a different language (eg C++) where you can access the file via pointer so you can rapidly move to the end by incrementing the pointer without having to read to a buffer.
Work out which disc sector the last segment of the file occupies then read the disc sector directly.  (The OP system might not let you do this.)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
XK8ER

so there is not really any other way huh?
ASKER CERTIFIED SOLUTION
Robert Schutt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AndyAinscow

Nice one - I'd been looking into the wrong classes and not found a way to set the position in the stream.  (Sometimes the documentation seems to be there to make things as difficult as possible to find).
Robert Schutt

@AndyAinscow, it can be troubling to navigate those pages, yes, especially when you're not certain where to look. I remembered there had to be a Seek but apart from having to find the exact class it is in, I also wondered if that would do exactly what you described: read through the whole file, which would make it rather useless of course. So after I found the right doc page, I decided to double check and test with a few fairly big files on a USB stick and the speed of that convinced me it was indeed jumping directly to the end, as it would do in C/C++.

@XK8ER, thanks for the grade!

PS: Now, looking back at my own test code, I found you can also simply replace Seek() with:
fs.Position = fs.Length - 1;

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.