mstester Posted November 23, 2006 Share Posted November 23, 2006 Hey guys,Ive a really stupid question to ask and I know I should know the answer 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 More sharing options...
jcarle Posted November 23, 2006 Share Posted November 23, 2006 http://www.ondotnet.com/pub/a/dotnet/2003/04/14/streams.html Link to comment Share on other sites More sharing options...
phiban Posted November 23, 2006 Share Posted November 23, 2006 Cheers jcarle, I take it that I would want to be using the Seek functionality yes? '---seekingfs.Seek(11, SeekOrigin.Current)fs.Read(bytes, 0, 6)For i = 0 To 5 Console.Write(Chr(bytes(i)))Nextfs.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 More sharing options...
phiban Posted November 23, 2006 Share Posted November 23, 2006 (edited) 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 November 23, 2006 by phiban Link to comment Share on other sites More sharing options...
jcarle Posted November 24, 2006 Share Posted November 24, 2006 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 More sharing options...
mstester Posted November 24, 2006 Author Share Posted November 24, 2006 (edited) Hi jcarle, sorry it took so long to reply. Thanks for the link it proved very useful however im having one slight little problen 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 :sI 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.IOPublic 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 FunctionEnd ClassThanks again tcp.txt Edited November 24, 2006 by mstester Link to comment Share on other sites More sharing options...
jcarle Posted November 24, 2006 Share Posted November 24, 2006 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 Link to comment Share on other sites More sharing options...
mstester Posted November 24, 2006 Author Share Posted November 24, 2006 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()'somethingloop"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 More sharing options...
jcarle Posted November 24, 2006 Share Posted November 24, 2006 fs.Read() will return >0 when there are bytes left to read. Link to comment Share on other sites More sharing options...
mstester Posted November 25, 2006 Author Share Posted November 25, 2006 Cheers dude Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now