Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi eveyone,

I've been working on this for days now and am frustrated... I keep getting an exception unhandled error "EndOfStreamException." It says "Unable to read beyond the end of the stream." What this code is supposed to do is that read a binary file but tis binary file has 4 byte floating-point values in it. What I am trying to do is just read these floating point values and write them to file... I would really appreciate it if someone could help me out. Thanks.

 

 Dim s1 As FileStream 'Load file 1
            Dim s3 As FileStream 'Save output

           If System.IO.File.Exists(TextBox19.Text) Then
               System.IO.File.Delete(TextBox19.Text)
           End If
           s1 = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
            s3 = New FileStream(TextBox19.Text, FileMode.CreateNew, FileAccess.Write)

           Dim br1 As BinaryReader
            Dim bw As BinaryWriter

           br1 = New BinaryReader(s1)
            bw = New BinaryWriter(s3)

           Dim fLen1 As Integer
           Dim f1 As New System.IO.FileInfo(TextBox1.Text)


                 fLen1 = s1.Length

 

           Dim snglRead1(fLen1) As Single
           Dim snglOutput(fLen1) As Single 'wıll hold results from division
              Dim i As Integer
           Dim j As Integer
           Dim k As Integer
           Dim m As Integer

           For i = 0 To fLen1 - 1
                    snglRead1(i) = br1.ReadSingle 'Exception thrown right here
                      Next


           

           For j = 0 To fLen1 - 1
               snglOutput(j) = snglRead1(j)
               bw.Write(snglOutput(j))
           Next

Posted

Your loop where the app throws the exception is going through every byte in the file,

while br1.readsingle reads 4 bytes every time it is called.

 

So dividing the length of the file by the length of the float (in case of single 4) should do the trick.

 

 

PS.:

Double posting is a bad thing :p

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...