You VB .NET developers out there,
I have posted 3 threads requesting help on reading floating point values from a binary file but did not get any reply at all. Is it because I am asking a stupid question or noone really have a clue about the question I asked? I am pretty sure that many of you know the answer. For the love of GOD, please give me some pointers on this. Following code just throws an exception at the first FOR NEXT loop where reading starts (Pls see the code below). I did manage reading integers from a binary file but just can't get it work to read floating point values.

Let me try to explain what I am trying to do. My program should read floating point values (values range between -1.00 and +1.00) from a binary file. After reading those floating point values, it will do some basic math with the values and then will write them into file.
Here is the code:

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 = f1.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


            s1.Close()
            s3.Close()
            MessageBox.Show("file created succesfully!", "Done")
            Me.Close()
        End If

Dani AI

Generated

The exception happened because the read loop used the file byte length as the loop count and BinaryReader.ReadSingle consumes 4 bytes per call, so the reader ran past the available bytes. was right to ask for the exception name (typically EndOfStreamException here), and ’s suggestion about matching the loop count to the number of Single values fixed the immediate problem. Below are safer patterns, edge-case checks, and practical tips to avoid similar problems and to handle large or mixed-format files.

A robust streaming pattern that does not require precomputing an exact count (and works for very large files) — read until there are at least 4 bytes left, process each value, and write out immediately to avoid large memory use:

Using fs As New FileStream(inputPath, FileMode.Open, FileAccess.Read)
  Using br As New BinaryReader(fs)
    Using outFs As New FileStream(outputPath, FileMode.Create, FileAccess.Write)
      Using bw As New BinaryWriter(outFs)
        While br.BaseStream.Position <= br.BaseStream.Length - 4
          Dim v As Single = br.ReadSingle()
          ' process v here
          bw.Write(v)
        End While
      End Using
    End Using
  End Using
End Using

Additional checks and cautions:

  • Confirm the file actually contains 4-byte floats (Single). If it contains doubles (8 bytes) or ASCII text, the read will fail or produce nonsense.
  • Endianness: .NET on x86/x64 is little-endian. If floats were written in big-endian, read 4 bytes, reverse them, then call BitConverter.ToSingle.
  • VB arrays use an inclusive upper bound — if you compute a count, allocate with Dim arr(count - 1) As Single or use a List(Of Single).
  • Catch and report exceptions (EndOfStreamException, IOException) during development so you see the real fault. For production, prefer streaming processing rather than loading whole files into memory.

These steps will make the reader/writer resilient and easier to debug beyond the single-byte-vs-element off-by-one issue.

Recommended Answers

All 3 Replies

>I have posted 3 threads requesting help on reading floating
>point values from a binary file but did not get any reply at all.
So you've flooded the forum with threads asking the same question? That alone will cause people to ignore you.

>Following code just throws an exception
What exception? That's somewhat important information.

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

maybe you trying to read while you reached the end of file ,

fLen1 = f1.Length

i think this return length on byte , try this

fLen1 = Int(f1.Length / 4)
commented: good catch +3

Thank you Manal for your reply. As you suggested, I divided f1.Length by 4 and it worked. I appreciate it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.