Question Filestream.Readbyte returns -1 value at the end of strem

MindConqueror

New member
Joined
Apr 7, 2021
Messages
4
Programming Experience
1-3
VB.NET:
Public Function writeverify(ByRef gb As Object, ByVal xflname As String)
    Try
        Dim onefilsize As Int64 = vars.flsize
        MessageBox.Show(onefilsize)
        Dim dataArray(onefilsize) As Byte

        'Dim randomGenerator As String = "1"
        Dim students(0) As Integer
        students(0) = 5
        'students(1) = 2

        '  randomGenerator.NextBytes(dataArray)

        Dim Filename As String = xflname

        MessageBox.Show(Filename)
        ' Dim numbers() = {1}

        Dim FileStream As FileStream = New FileStream(Filename, FileMode.Create)

        Try
            For i As Integer = 0 To dataArray.Length - 1
                Try
                    FileStream.WriteByte(students(0))
                Catch ex As Exception
                End Try
            Next i

            FileStream.Seek(0, SeekOrigin.Begin)

            For i As Integer = 0 To CType(FileStream.Length, Integer)
                RichTextBox1.AppendText(FileStream.ReadByte())

                If (students(0)) <> (FileStream.ReadByte()) Then
                    MessageBox.Show("Error writing data.")
                    Return ""
                End If
            Next i
        Finally
            ' FileStream.Seek(SeekOrigin.Begin, SeekOrigin.End)
            FileStream.Close()
        End Try

        Return ""
    Catch ex As UnauthorizedAccessException
        MessageBox.Show("You dont have write access to wipe the disk.")
    End Try

    Return ""
End Function
but during verify its returning -1 value in end how i can remmove it
 
Last edited by a moderator:
Firstly, please don't post unformatted code. Make the effort to help us help you by making your code as readable as possible. That means formatting it and, among other things, not including loads of pointless whitespace.

Secondly, please provide a FULL and CLEAR explanation of the problem. You haven't explained what you're actually trying to achieve and it's not for us to work it out from code that doesn't do it.

As for the question, of course that's what FileStream.ReadByte does. It says so right there in the documentation. If that's not what you want then don't call that method. Again, you haven't actually explained what you're trying to achieve so how are we supposed to know how to "fix" your code to achieve it? Don't try to get away with doing as little as possible. If you'd like us to spend our time to help you, you need to spend your time to help us do so, which includes a full explanation of the problem and easily readable and relevant code.
 
am trying to write a file with value 1 and than want to verify that the data is written is file is correct , so its a write and verify method
 
Public Function writeverify(ByRef gb As Object, ByVal xflname As String)
Try
Dim onefilsize As Int64 = vars.flsize
MessageBox.Show(onefilsize)
Dim dataArray(onefilsize) As Byte


Dim students(0) As Integer
students(0) = 5




Dim Filename As String = xflname

MessageBox.Show(Filename)


Dim FileStream As FileStream = New FileStream(Filename, FileMode.Create)

Try
For i As Integer = 0 To dataArray.Length
Try
FileStream.WriteByte(students(0))
Catch ex As Exception
End Try
Next i

FileStream.Seek(0, SeekOrigin.Begin)

For i As Integer = 0 To CType(FileStream.Length, Integer)
RichTextBox1.AppendText(FileStream.ReadByte())

If students(0) <> (FileStream.ReadByte()) Then
MessageBox.Show("Error writing data.")
Return ""
End If
Next i
Finally

FileStream.Close()
End Try

Return ""
Catch ex As UnauthorizedAccessException
MessageBox.Show("You dont have write access to wipe the disk.")
End Try

Return ""
End Function
 
this function is returning this on varify
555555555555555555555555555555555555555555555555555555555555-1

but i coded it to write only 5

this magic value -1 is comming how i can remove this magic value
 
I specifically requested that you don't post unformatted code or code with chunks of useless whitespace and then you immediately do both. Making it difficult for people to help you is not the best way to get help.
 
this function is returning this on varify
555555555555555555555555555555555555555555555555555555555555-1

but i coded it to write only 5

this magic value -1 is comming how i can remove this magic value
There's no "removing" it. The whole point is that you call ReadByte and if you get a result of -1 then you have arrived at the end of the file so you should stop, otherwise you can use the result you get. You are obviously using that value when you should not be. You need something that will test whether the result is -1. Generally speaking, you would use a Do or While loop that exits when the result is -1.

Try thinking about the logic first, then writing code only when you already have working logic. Write down the steps of the algorithm if you need to, then follow the steps manually to see whether they work. Once your manual tests work, then you can write code to implement those steps. If your manual tests don't use that -1 value in the output then neither will your code, or else the code doesn't actually implement that algorithm.
 
You have 2 ReadByte calls in the loop, each one advancing one byte. If you want to do 2 things with the same byte (display it and compare it) then you must call ReadByte only once and assign it to a variable.
pseudo:
for i = 0 to (length-1)
   dim value = readbyte
   display value
   compare value
next
 
Back
Top