473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to only read the last 100 bytes from a Binary file?

Hi,

Is it possible to read a file in reverse and only get the last 100 bytes in
the file without reading the whole file from the begining? I have to get info
from files that are in the last 100 bytes of the file and some of these files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100 bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the file
resulting in upper bounds of maybe 200 000 000, so this is time consuming to
get the last 100 bytes.

Any idéas?
Thanks /Thomas
Feb 1 '07 #1
6 5764
Try seeking to oFileStream.Length - 100. Then read from there to the end of
the file. The array size only needs to be 100 bytes. It may take the OS a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
Hi,

Is it possible to read a file in reverse and only get the last 100 bytes
in
the file without reading the whole file from the begining? I have to get
info
from files that are in the last 100 bytes of the file and some of these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the file
resulting in upper bounds of maybe 200 000 000, so this is time consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas

Feb 1 '07 #2
Ahh, thanks for the "Eye opener"
I used the binaryreader instead and set the "cursor" to the End -100
position and step to the end.

This code worked for me:

Dim fileData(100) As Byte
Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fdi As Integer = 0
For i As Long = 0 To 99
fileData(i) = oBinaryReader.ReadByte()
Next i
oBinaryReader.Close()
oFileStream.Close()

Thanx
/Thomas

"Michael D. Ober" wrote:
Try seeking to oFileStream.Length - 100. Then read from there to the end of
the file. The array size only needs to be 100 bytes. It may take the OS a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
Hi,

Is it possible to read a file in reverse and only get the last 100 bytes
in
the file without reading the whole file from the begining? I have to get
info
from files that are in the last 100 bytes of the file and some of these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the file
resulting in upper bounds of maybe 200 000 000, so this is time consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas


Feb 1 '07 #3
Out of curiosity, is there a performance difference when seeking to end -100
on a 1K file vs a 10Gb file. I know the old FAT file system would take a
lot longer for the seek on the larger file. Also, why not use ReadBytes and
avoid your read loop?

Dim oBinaryReader As New BinaryReader(New FileStream(FilePath,
FileMode.Open, FileAccess.Read))
oBinaryReader.BaseStream.Position = oBinaryReader.BaseStream.Length - 100
Dim fileData(100) As Byte = oBinaryReader.ReadBytes(100)
oBinaryReader.Close() ' Close both the binary reader and the base
stream

Mike.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
Ahh, thanks for the "Eye opener"
I used the binaryreader instead and set the "cursor" to the End -100
position and step to the end.

This code worked for me:

Dim fileData(100) As Byte
Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fdi As Integer = 0
For i As Long = 0 To 99
fileData(i) = oBinaryReader.ReadByte()
Next i
oBinaryReader.Close()
oFileStream.Close()

Thanx
/Thomas

"Michael D. Ober" wrote:
>Try seeking to oFileStream.Length - 100. Then read from there to the end
of
the file. The array size only needs to be 100 bytes. It may take the OS
a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microso ft.com...
Hi,

Is it possible to read a file in reverse and only get the last 100
bytes
in
the file without reading the whole file from the begining? I have to
get
info
from files that are in the last 100 bytes of the file and some of these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the
file
resulting in upper bounds of maybe 200 000 000, so this is time
consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas



Feb 1 '07 #4
Regardless of the file size it takes about 156 milliseconds to get the last
100 bytes.
Measured with a 100kb file and a 656MB file over the network. This is very
nice since I have to read hundreds of files on a regular basis.

I get this error when trying youre example with ReadBytes(100) "Explicit
initialization is not permitted for arrays declared with explicit bounds."
But the idéa is good and I'll look further into it.

Thanks again
/Thomas

"Michael D. Ober" wrote:
Out of curiosity, is there a performance difference when seeking to end -100
on a 1K file vs a 10Gb file. I know the old FAT file system would take a
lot longer for the seek on the larger file. Also, why not use ReadBytes and
avoid your read loop?

Dim oBinaryReader As New BinaryReader(New FileStream(FilePath,
FileMode.Open, FileAccess.Read))
oBinaryReader.BaseStream.Position = oBinaryReader.BaseStream.Length - 100
Dim fileData(100) As Byte = oBinaryReader.ReadBytes(100)
oBinaryReader.Close() ' Close both the binary reader and the base
stream

Mike.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
Ahh, thanks for the "Eye opener"
I used the binaryreader instead and set the "cursor" to the End -100
position and step to the end.

This code worked for me:

Dim fileData(100) As Byte
Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fdi As Integer = 0
For i As Long = 0 To 99
fileData(i) = oBinaryReader.ReadByte()
Next i
oBinaryReader.Close()
oFileStream.Close()

Thanx
/Thomas

"Michael D. Ober" wrote:
Try seeking to oFileStream.Length - 100. Then read from there to the end
of
the file. The array size only needs to be 100 bytes. It may take the OS
a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
Hi,

Is it possible to read a file in reverse and only get the last 100
bytes
in
the file without reading the whole file from the begining? I have to
get
info
from files that are in the last 100 bytes of the file and some of these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the
file
resulting in upper bounds of maybe 200 000 000, so this is time
consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas


Feb 2 '07 #5
Got it, Dim as Byte array:

Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fileData As Byte() = oBinaryReader.ReadBytes(100)
oBinaryReader.Close()
oFileStream.Close()

Did you mean that oBinaryReader.Close automatically closes oFileStream.Close?

"Michael D. Ober" wrote:
Out of curiosity, is there a performance difference when seeking to end -100
on a 1K file vs a 10Gb file. I know the old FAT file system would take a
lot longer for the seek on the larger file. Also, why not use ReadBytes and
avoid your read loop?

Dim oBinaryReader As New BinaryReader(New FileStream(FilePath,
FileMode.Open, FileAccess.Read))
oBinaryReader.BaseStream.Position = oBinaryReader.BaseStream.Length - 100
Dim fileData(100) As Byte = oBinaryReader.ReadBytes(100)
oBinaryReader.Close() ' Close both the binary reader and the base
stream

Mike.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
Ahh, thanks for the "Eye opener"
I used the binaryreader instead and set the "cursor" to the End -100
position and step to the end.

This code worked for me:

Dim fileData(100) As Byte
Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fdi As Integer = 0
For i As Long = 0 To 99
fileData(i) = oBinaryReader.ReadByte()
Next i
oBinaryReader.Close()
oFileStream.Close()

Thanx
/Thomas

"Michael D. Ober" wrote:
Try seeking to oFileStream.Length - 100. Then read from there to the end
of
the file. The array size only needs to be 100 bytes. It may take the OS
a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
Hi,

Is it possible to read a file in reverse and only get the last 100
bytes
in
the file without reading the whole file from the begining? I have to
get
info
from files that are in the last 100 bytes of the file and some of these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the
file
resulting in upper bounds of maybe 200 000 000, so this is time
consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas


Feb 2 '07 #6
According to the documentation, BinaryReader.Close() closes both the binary
reader and the underlying stream. That's why you can use a single Binary
Reader and access it's BaseStream property to get the file length and set
your desired position.

Mike.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:1A**********************************@microsof t.com...
Got it, Dim as Byte array:

Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fileData As Byte() = oBinaryReader.ReadBytes(100)
oBinaryReader.Close()
oFileStream.Close()

Did you mean that oBinaryReader.Close automatically closes
oFileStream.Close?

"Michael D. Ober" wrote:
>Out of curiosity, is there a performance difference when seeking to
end -100
on a 1K file vs a 10Gb file. I know the old FAT file system would take a
lot longer for the seek on the larger file. Also, why not use ReadBytes
and
avoid your read loop?

Dim oBinaryReader As New BinaryReader(New FileStream(FilePath,
FileMode.Open, FileAccess.Read))
oBinaryReader.BaseStream.Position = oBinaryReader.BaseStream.Length - 100
Dim fileData(100) As Byte = oBinaryReader.ReadBytes(100)
oBinaryReader.Close() ' Close both the binary reader and the
base
stream

Mike.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:34**********************************@microso ft.com...
Ahh, thanks for the "Eye opener"
I used the binaryreader instead and set the "cursor" to the End -100
position and step to the end.

This code worked for me:

Dim fileData(100) As Byte
Dim oFileStream As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim oBinaryReader As New BinaryReader(oFileStream)
Dim lBytes As Long = oFileStream.Length
oBinaryReader.BaseStream.Position = lBytes - 100
Dim fdi As Integer = 0
For i As Long = 0 To 99
fileData(i) = oBinaryReader.ReadByte()
Next i
oBinaryReader.Close()
oFileStream.Close()

Thanx
/Thomas

"Michael D. Ober" wrote:

Try seeking to oFileStream.Length - 100. Then read from there to the
end
of
the file. The array size only needs to be 100 bytes. It may take the
OS
a
while to do the seek, but you won't get the out of memory errors.

Mike Ober.

"ThomasZ" <Th*****@discussions.microsoft.comwrote in message
news:4C**********************************@microso ft.com...
Hi,

Is it possible to read a file in reverse and only get the last 100
bytes
in
the file without reading the whole file from the begining? I have to
get
info
from files that are in the last 100 bytes of the file and some of
these
files
are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on
the
largest files and the other files take "forever" to get the last 100
bytes.

This is the code I have currently that works with smaller files:

Dim fileData() As Byte
FilePath = "C:\Temp\1DD04336CA1A45CB81A89167048D2594.abk"
Try
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo(FilePath)
Dim oFileStream As System.IO.FileStream = oFile.Open
_(IO.FileMode.Open, IO.FileAccess.Read)
Dim lBytes As Long = oFileStream.Length
ReDim fileData(lBytes)
Dim StartReadAt As Long = lBytes - 100
Dim StopReadAt As Long = 100
oFileStream.Read(fileData, StartReadAt, StopReadAt)
oFileStream.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The problem with this code is an Array is created in the size of the
file
resulting in upper bounds of maybe 200 000 000, so this is time
consuming
to
get the last 100 bytes.

Any idéas?
Thanks /Thomas



Feb 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Axel | last post by:
Hiho, I want to read the content of a binary file into a string. It works, but only the first 4 chars are left after the reading and the stringsize is, of course, 4 bytes. I got this method...
4
by: Vladimir | last post by:
Can anyone halp me with some questions? Is there multiplatform standart in binary representation of primitive datatypes when you writing it through BinaryWriter? In other words if I write by...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
13
by: Gary Wessle | last post by:
Hi there I have a method which returns time_t and another two methods return double data types and I cann't change that since the library is provided by Big Bucks Inc. I think time_t is long but...
4
by: Mastastealth | last post by:
I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next...
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.