Hi all,
I am trying to make a program in VB .NET that has two "browse" bottons, two textboxes which should show the full path of the files to be selected, one "Save As" button, one textbox that should show the file's full path where to be saved in, an "OK" button, and a "Cancel" button. By the way files are binary.

When the program starts executing, the user will select (by using open file dialog) two different binary files(file1 and file2 for example). Then the user should give a name to save the file and select the directory where to be saved by clicking Save As button.. Then the user should click Ok button. Once the OK button clicked, first value (colum1,row1) of file1 should be added to first value of file2 (colum1,row1) and this will continue till the end of both files. Once the addition ends, the resulted output file should be saved into a directory where the user prompts. Is this program very hard to code for a beginner? Or does anyone know where I can get a similar code?

I would greatly appreciate any help.

The column1, row1 stuff kind of confusses me but I think that you are wanting to read a val from file1 and another from file2 and put the resulting value into another file. If so you would use a loop to read file1 to the end while getting a line from file2. I haven't tested this but I believe that it could work or at least get you started, I'm assuming that the length of both files are the same;

'make a file stream for writing out the value
        Dim fs As New System.IO.FileStream("yourfile.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write)
        'make a stream to hold the values
        Dim streamOut As New System.IO.BinaryWriter(fs)
        Dim srFile1 As New System.IO.FileStream("file1.txt", IO.FileMode.Open)
        Dim srFile2 As New System.IO.FileStream("file2.txt", IO.FileMode.Open)
        'create readers to get the values
        Dim streamFile1 As New System.IO.BinaryReader(srFile1)
        Dim streamFile2 As New System.IO.BinaryReader(srFile2)
        'vars to hold values
        Dim iVal1, iVal2, iVal3 As Integer

        Try
            'read from the first file to the end
            For intJ As Integer = 0 To streamFile1.BaseStream.Length - 1
                'get values
                iVal1 = streamFile1.ReadInt32
                iVal2 = streamFile2.ReadInt32
                'put vals together
                iVal3 = iVal1 + iVal2
                'write to the output file
                streamOut.Write(iVal3)
            Next

            'close the files
            streamFile1.Close()
            streamFile2.Close()
            streamOut.Close()

        Catch ex As Exception
            Throw ex
        End Try

Thanks emurf for the code you posted. I have another question. If the user clicks OK button first instead of loading input files, I get an error filenotfoundexception error. I am trying to add a message saying "Please enter a file first" and textbox1 should be focused and should be waiting for the user to enter a file. Below my code simply dies when I click OK button without selecting input file first.

'Load/Select input file button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
openFD.InitialDirectory = "C:\"
openFD.Multiselect = False
openFD.Title = "Select first image file"
openFD.Filter = "All Files [*.*]| *.*"
' Make sure the User clicked OK and not Cancel
If (openFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox1.Text = ""
Dim loadFileName As String = openFD.FileName
'show the full path of selected file
TextBox1.AppendText(loadFileName)
End If

End Sub

'Ok button code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Call testRun()
Me.Close()

End Sub

Public Function testRun()
Dim s1 As FileStream
Dim s2 As FileStream

'---read from and write to a binary file
s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s2 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)

Dim br As BinaryReader
Dim bw As BinaryWriter
br = New BinaryReader(s1)
bw = New BinaryWriter(s2)

Dim byteRead As Byte
Dim j As Integer
For j = 0 To br.BaseStream.Length() - 1
byteRead = br.ReadByte
bw.Write(byteRead)
Next
br.Close()
bw.Close()
Return byteRead

End Function

There are two ways to check the input. One way is to make sure that there is a string in the textbox and the other is to make sure that the filepath exists. The look something like this;

'Make sure that something is in the textbox
If Len(Me.Textbox1.Text.Trim) = 0 Then
    MessageBox.Show("Please enter a file first", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'set focus to textbox1
Me.Textbox1.Focus
End If

'This validates the filepath, done after you check to make sure that the user entered someting in textbox1
If Not System.IO.File.Exists(Me.Textbox1.Text.Trim) Then
    MessageBox.Show("The file entered is invalid", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'set focus to textbox1
Me.Textbox1.Focus
End If

Thanks a lot for the code emurf.

Hi guys
In a standart save file dialog, if the filename exists during a saving process, I select overwrite button but it throws filealreadyexists exception and program dies. How could I make that when the user clicks overwrite button the program will not die and it would actually overwrite the previosly existing filename? Thanks.

You would do that with the stream writer object like this, Dim swWriter As New System.IO.StreamWriter("textfile.txt", False) . The second parameter is append. If it is false then the file is overwritten, if it is true then what you are writing is added to the end of the file.

If for some reason that doesn't work or you just cant do that, you may not be able to in the code that I wrote above, you cand do this If System.IO.File.Exists("filepath") Then System.IO.File.Delete("filepath") before you create and write your file.

Thank you, emurf.

Hi emurf. My input files are single-band satellite images where each pixel has a value of between 0 and 255. As you know I use binaryreader and binarywriter objects to read and write to a file. Up tu this point everything is ok. The point I can't understand is what to do right after the input files are read. I am supposed to apply this simple division: (inputfile1/inputfile2) that is each pixel value of inputfile1 will be divided by corresponding pixel value of inputfile2. Here are 2 points where I am stuck with:
1-Would it make sense to create a 2-dimensional array for each inputfile and read them using binaryreader and binarywriter objects? Is there any simpler way to do this at all?
2-Since both images could have a value of zero, I could most likely get a division by zero error during division operation. How could I resolve this?
Thanks in advance.

It is possible to use a 2 dimensional array. Before declaring the array you should find the length of the file and use that as the size of the array. Then do something like this.

Dim dblFiles(intFileLen, 1) As Double
Dim intI As Integer, dblOut As Double

Try
    'read the files 
    For intI = 0 To intFileLen - 1
        'get the vals & put into the array
         dblFiles(intI, 0) = streamFile1.ReadInt16
         dblFiles(intI, 1) = streamFile2.ReadInt16
     Next

     'write the vals
      For intI = 0 To intFileLen - 1
          'check the vals for division by 0
          If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then
              dblOut = 0
          Else 'no division error, get the val to write
              dblOut = dblFiles(intI, 0) / dblFiles(intI, 1)
          End If
          'write the val
           streamOut.Write(dblOut)
       Next

Catch ex As Exception
       Throw ex
End Try

The program will run a little faster if you write the if statements checking for divid by zero in the read loop and write to the file there (in the first set of code at the top of the page).

Thanks emurf for your quick reply. When I run the code you posted, I get an exception saying "Unable to read beyond the end of the stream." This is EndOfStreamException. Where do you think I am making a mistake? Here is the code:

Public Function testRun()
Dim s1 As FileStream 'Load binaryfile1
Dim s2 As FileStream 'Load binaryfile2
Dim s3 As FileStream 'Save output file

'---read from and write to a binary file
s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s2 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s3 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)

Dim br1 As BinaryReader
Dim br2 As BinaryReader
Dim bw As BinaryWriter

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

Dim f1 As New System.IO.FileInfo(openFD.FileName)
Dim intFileLen As Integer
intFileLen = f1.Length
Dim dblFiles(intFileLen, 1) As Double
Dim intI As Integer, dblOut As Double

Try
'read the files
For intI = 0 To intFileLen - 1
'get the vals & put into the array
dblFiles(intI, 0) = br1.ReadInt16
dblFiles(intI, 1) = br2.ReadInt16
Next

'write the vals
For intI = 0 To intFileLen - 1
'check the vals for division by 0
If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then
dblOut = 0
Else 'no division error, get the val to write
dblOut = dblFiles(intI, 0) / dblFiles(intI, 1)
End If
'write the val
bw.Write(dblOut)
Next

Catch ex As Exception
Throw ex
End Try

br1.Close()
br2.Close()
bw.Close()
Return dblOut
End Function

Hi emurf. I tried your suggestion but I keep getting EndOfStreamException which is unable to read beyond the end of the stream. My inputfile has 112 rows, 112 columns that's 12544 elements or pixels. I checked the code I am using many times but can not find where I am doing wrong. Here is the code I am using:

Public Function testRun()
Dim s1 As FileStream 'Load file1
Dim s2 As FileStream 'Load file2
Dim s3 As FileStream 'Save output file

'---read from and write to a binary file
s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s2 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
s3 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)

Dim br1 As BinaryReader
Dim br2 As BinaryReader
Dim bw As BinaryWriter

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

Dim f1 As New System.IO.FileInfo(openFD.FileName)
Dim intFileLen As Integer
intFileLen = f1.Length
Dim dblFiles(intFileLen, 1) As Double
Dim intI As Integer, dblOut As Double

Try
'read the files
For intI = 0 To intFileLen - 1
'get the vals & put into the array
dblFiles(intI, 0) = br1.ReadInt16
dblFiles(intI, 1) = br2.ReadInt16
Next

'write the vals
For intI = 0 To intFileLen - 1
'check the vals for division by 0
If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then
dblOut = 0
Else 'no division error, get the val to write
dblOut = dblFiles(intI, 0) / dblFiles(intI, 1)
End If
'write the val
bw.Write(dblOut)
Next

Catch ex As Exception
Throw ex
End Try

br1.Close()
br2.Close()
bw.Close()
Return dblOut
End Function

I was able to make this work on a gif by changing the array to a byte array and readInt16 to readByte. See if that works.

Dim s1 As FileStream 'Load file1
        Dim s2 As FileStream 'Load file2
        Dim s3 As FileStream 'Save output file

        '---read from and write to a binary file
        s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
        s2 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
        s3 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)


        Dim br1 As BinaryReader
        Dim br2 As BinaryReader
        Dim bw As BinaryWriter

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

        Dim f1 As New System.IO.FileInfo(openFD.FileName)
        Dim intFileLen As Integer
        intFileLen = f1.Length
        Dim dblFiles(intFileLen, 1) As Double
        Dim bytFiles(intFileLen, 1) As Byte
        Dim intI As Integer, dblOut As Double

        Try
            'read the files 
            For intI = 0 To intFileLen - 1
                'get the vals & put into the array
                bytFiles(intI, 0) = br1.ReadByte
                bytFiles(intI, 1) = br2.ReadByte
            Next


            'write the vals
            For intI = 0 To intFileLen - 1
                'check the vals for division by 0
                If bytFiles(intI, 0) = 0 Or bytFiles(intI, 1) = 0 Then
                    dblOut = 0
                Else
                    dblOut = bytFiles(intI, 0) / bytFiles(intI, 1)
                End If
                'write the val
                bw.Write(dblOut)
            Next

        Catch ex As Exception
            Throw ex
        End Try

        br1.Close()
        br2.Close()
        bw.Close()

        Return dblOut

It is possible to use a 2 dimensional array. Before declaring the array you should find the length of the file and use that as the size of the array. Then do something like this.

Dim dblFiles(intFileLen, 1) As Double
Dim intI As Integer, dblOut As Double

Try
    'read the files 
    For intI = 0 To intFileLen - 1
        'get the vals & put into the array
         dblFiles(intI, 0) = streamFile1.ReadInt16
         dblFiles(intI, 1) = streamFile2.ReadInt16
     Next

     'write the vals
      For intI = 0 To intFileLen - 1
          'check the vals for division by 0
          If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then
              dblOut = 0
          Else 'no division error, get the val to write
              dblOut = dblFiles(intI, 0) / dblFiles(intI, 1)
          End If
          'write the val
           streamOut.Write(dblOut)
       Next

Catch ex As Exception
       Throw ex
End Try

The program will run a little faster if you write the if statements checking for divid by zero in the read loop and write to the file there (in the first set of code at the top of the page).

Hi All


Am young in vb

i have got one problem, i designed a form that will be used for loan payment. The payment will be in a sequential order of month. My aim i want to make deduction if customer refuse to pay in previous month.

How can i do 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.