Link to home
Start Free TrialLog in
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?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of XK8ER

ASKER

is there a faster way to just read the last bytes?
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.)
Avatar of XK8ER

ASKER

so there is not really any other way huh?
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
@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