MSFN Forum: VB.net, Reading 'X' number of Bytes from a file - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

VB.net, Reading 'X' number of Bytes from a file Dumb question!! Rate Topic: -----

#1 User is offline   mstester 

  • Newbie
  • Group: Members
  • Posts: 25
  • Joined: 17-July 06

Posted 22 November 2006 - 08:47 PM

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,


#2 User is offline   jcarle 

  • MSFN Master
  • Group: Developers
  • Posts: 2,569
  • Joined: 14-August 04

Posted 22 November 2006 - 09:18 PM

http://www.ondotnet....14/streams.html

#3 User is offline   phiban 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 01-October 06

Posted 23 November 2006 - 07:59 AM

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?

#4 User is offline   phiban 

  • Newbie
  • Group: Members
  • Posts: 17
  • Joined: 01-October 06

Posted 23 November 2006 - 08:37 AM

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?

This post has been edited by phiban: 23 November 2006 - 11:24 AM


#5 User is offline   jcarle 

  • MSFN Master
  • Group: Developers
  • Posts: 2,569
  • Joined: 14-August 04

Posted 23 November 2006 - 06:01 PM

View Postphiban, on Nov 23 2006, 09:37 AM, said:

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)

#6 User is offline   mstester 

  • Newbie
  • Group: Members
  • Posts: 25
  • Joined: 17-July 06

Posted 23 November 2006 - 10:40 PM

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 :)

Attached File(s)

  • Attached File  tcp.txt (800bytes)
    Number of downloads: 5

This post has been edited by mstester: 23 November 2006 - 10:46 PM


#7 User is offline   jcarle 

  • MSFN Master
  • Group: Developers
  • Posts: 2,569
  • Joined: 14-August 04

Posted 23 November 2006 - 11:08 PM

For one, get rid of that whole calc function. Read the bytes until EOF in 80 byte chunks.

Second, you do not need to seek. When you first open the file, it is already at position 0. Everytime you Read, the cursor advances in the stream by the size of the read.

http://msdn2.microsoft.com/en-us/library/s...tream.read.aspx

#8 User is offline   mstester 

  • Newbie
  • Group: Members
  • Posts: 25
  • Joined: 17-July 06

Posted 24 November 2006 - 11:01 AM

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

#9 User is offline   jcarle 

  • MSFN Master
  • Group: Developers
  • Posts: 2,569
  • Joined: 14-August 04

Posted 24 November 2006 - 11:40 AM

fs.Read() will return >0 when there are bytes left to read.

#10 User is offline   mstester 

  • Newbie
  • Group: Members
  • Posts: 25
  • Joined: 17-July 06

Posted 24 November 2006 - 08:32 PM

Cheers dude :)

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy