Jump to content

VB.net, Reading 'X' number of Bytes from a file


Recommended Posts

Hey guys,

Ive a really stupid question to ask and I know I should know the answer :blushing: but anyway. Lets say I have a txt file of size 'x', how would I read for example 80 bytes at a time and then end once the method reaches the EOF?

Any Idea??

Thanks,

Link to comment
Share on other sites


Cheers jcarle, I take it that I would want to be using the Seek functionality yes?

'---seeking
fs.Seek(11, SeekOrigin.Current)
fs.Read(bytes, 0, 6)
For i = 0 To 5
Console.Write(Chr(bytes(i)))
Next
fs.Close()

In the above example the starting position is 11, i take it that if i was implementing this concept i would have the index at 0 not 11 correct?

Link to comment
Share on other sites

sorry i didnt explain my self right in that last post, what i meant to say was:

If i had a file like Mstest does, how would I read the first 80 bytes and then the second 80 and so on?? would i use the fs.seek method?

Edited by phiban
Link to comment
Share on other sites

sorry i didnt explain my self right in that last post, what i meant to say was:

If i had a file like Mstest does, how would I read the first 80 bytes and then the second 80 and so on?? would i use the fs.seek method?

Using an 80 byte byte array, you would read 80 bytes at a time in the stream:

fs.Read(bytes, 0, 80)

Link to comment
Share on other sites

Hi jcarle, sorry it took so long to reply.

Thanks for the link it proved very useful :D however im having one slight little problen :blink: Basically what happening is my code doesnt seem to read all the bytes in the txt file that im trying to read. I have posted up both the file and code which im using. Just to fill you in a bit more on what im trying to do:

I have a txt file lets say called tcp.txt (I just copied part of a random article from wiki), I'm trying to read in the first 80 bytes of that file and write them to a new file which get created before hand (called tcpCopy.txt). Once the first 80 bytes have been read and wrote to the new txt file, I need it to read the next 80, write them and so on till the EOF is found.

It seems to be reading the first 80 fine but then it goes pair shaped on me :s

I have a function called calc, it basically gets the file size (in bytes) of the .txt and divides it by 80 the number of bytes needed, it uses this calculation to loop through the code which reads the data. So if a file is 800bytes it loops 10 times. I know that a better way would be to have the stream read all the bytes in the file but however this is not a option :(

If there's any light you or anybody else could shed on this, I would be most greatfull.

Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
'name of file to read from
Const file_name = "C:\Documents and Settings\user\Desktop\tcp.txt"
'name of file to write to
Const filename1 = "C:\Documents and Settings\user\Desktop\tcpCopy.txt"
'stream to read from
Dim fs = New FileStream(file_name, FileMode.Open, FileAccess.Read)
'Stream to write to
Dim fileStream As fileStream = New fileStream(filename1, FileMode.Create)
'Byte Array of size 80
Dim bytes(80) As Byte

+Region " Windows Form Designer generated code "


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
calc()
ReadByte()

End Sub
Private Sub ReadByte()
'Count is used to loop until all data has been collected
Dim count As Integer
'Var incrementPos used to increment the position pointer in File i.e. After reading the first 80 bytes
'set the position to 80 and read the next 80 byte and so on
Dim incrememtPos As Integer

count = 0

Do Until count = calc()

fs.Seek(incrememtPos, SeekOrigin.Current)
fs.Read(bytes, 0, 80)
For i As Integer = 0 To bytes.Length - 1
fileStream.WriteByte(bytes(i))

Next i

bytes.Clear(bytes, 0, 80)
count = count + 1
incrememtPos = incrememtPos + 80

Loop
fileStream.Close()
End Sub
'This Function preforms a calculation of a given file size and returns the size
'number and hence the number of passes needed to take all the data in from
'the txt file

Public Function calc() As Integer
Dim MySize As Long
MySize = FileLen(file_name)
Return MySize / 80

End Function
End Class

Thanks again :)

tcp.txt

Edited by mstester
Link to comment
Share on other sites

Cheers jcarle, it works fine now, I have one other question though if i wanted to loop until the EOF was found would i do something like so

"Do while not EOF()

'something

loop"

However I know that the EOF() requires "An Integer containing any valid file number" and im not too sure how this would apply to my case (in my code snippet above)

Thanks again

P.S. im sorry for bugging you with all these stupid questions

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...