Help for file handling in VB. NET

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Help for file handling in VB. NET

 
0
  #1
Dec 2nd, 2007
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.
Attached Thumbnails
snapshot of my program.JPG  
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 37
Reputation: emurf is an unknown quantity at this point 
Solved Threads: 4
emurf emurf is offline Offline
Light Poster

Re: Help for file handling in VB. NET

 
0
  #2
Dec 3rd, 2007
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;
  1. 'make a file stream for writing out the value
  2. Dim fs As New System.IO.FileStream("yourfile.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write)
  3. 'make a stream to hold the values
  4. Dim streamOut As New System.IO.BinaryWriter(fs)
  5. Dim srFile1 As New System.IO.FileStream("file1.txt", IO.FileMode.Open)
  6. Dim srFile2 As New System.IO.FileStream("file2.txt", IO.FileMode.Open)
  7. 'create readers to get the values
  8. Dim streamFile1 As New System.IO.BinaryReader(srFile1)
  9. Dim streamFile2 As New System.IO.BinaryReader(srFile2)
  10. 'vars to hold values
  11. Dim iVal1, iVal2, iVal3 As Integer
  12.  
  13. Try
  14. 'read from the first file to the end
  15. For intJ As Integer = 0 To streamFile1.BaseStream.Length - 1
  16. 'get values
  17. iVal1 = streamFile1.ReadInt32
  18. iVal2 = streamFile2.ReadInt32
  19. 'put vals together
  20. iVal3 = iVal1 + iVal2
  21. 'write to the output file
  22. streamOut.Write(iVal3)
  23. Next
  24.  
  25. 'close the files
  26. streamFile1.Close()
  27. streamFile2.Close()
  28. streamOut.Close()
  29.  
  30. Catch ex As Exception
  31. Throw ex
  32. End Try
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Re: Help for file handling in VB. NET

 
0
  #3
Dec 4th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 37
Reputation: emurf is an unknown quantity at this point 
Solved Threads: 4
emurf emurf is offline Offline
Light Poster

Re: Help for file handling in VB. NET

 
0
  #4
Dec 4th, 2007
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;

  1. 'Make sure that something is in the textbox
  2. If Len(Me.Textbox1.Text.Trim) = 0 Then
  3. MessageBox.Show("Please enter a file first", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  4. 'set focus to textbox1
  5. Me.Textbox1.Focus
  6. End If
  7.  
  8. 'This validates the filepath, done after you check to make sure that the user entered someting in textbox1
  9. If Not System.IO.File.Exists(Me.Textbox1.Text.Trim) Then
  10. MessageBox.Show("The file entered is invalid", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  11. 'set focus to textbox1
  12. Me.Textbox1.Focus
  13. End If
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Re: Help for file handling in VB. NET

 
0
  #5
Dec 4th, 2007
Thanks a lot for the code emurf.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Re: Help for file handling in VB. NET

 
0
  #6
Dec 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 37
Reputation: emurf is an unknown quantity at this point 
Solved Threads: 4
emurf emurf is offline Offline
Light Poster

Re: Help for file handling in VB. NET

 
0
  #7
Dec 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Re: Help for file handling in VB. NET

 
0
  #8
Dec 6th, 2007
Thank you, emurf.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 19
Reputation: cs_tx_usa is an unknown quantity at this point 
Solved Threads: 0
cs_tx_usa's Avatar
cs_tx_usa cs_tx_usa is offline Offline
Newbie Poster

Re: Help for file handling in VB. NET

 
0
  #9
Dec 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 37
Reputation: emurf is an unknown quantity at this point 
Solved Threads: 4
emurf emurf is offline Offline
Light Poster

Re: Help for file handling in VB. NET

 
0
  #10
Dec 7th, 2007
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.

  1. Dim dblFiles(intFileLen, 1) As Double
  2. Dim intI As Integer, dblOut As Double
  3.  
  4. Try
  5. 'read the files
  6. For intI = 0 To intFileLen - 1
  7. 'get the vals & put into the array
  8. dblFiles(intI, 0) = streamFile1.ReadInt16
  9. dblFiles(intI, 1) = streamFile2.ReadInt16
  10. Next
  11.  
  12. 'write the vals
  13. For intI = 0 To intFileLen - 1
  14. 'check the vals for division by 0
  15. If dblFiles(intI, 0) = 0 Or dblFiles(intI, 1) = 0 Then
  16. dblOut = 0
  17. Else 'no division error, get the val to write
  18. dblOut = dblFiles(intI, 0) / dblFiles(intI, 1)
  19. End If
  20. 'write the val
  21. streamOut.Write(dblOut)
  22. Next
  23.  
  24. Catch ex As Exception
  25. Throw ex
  26. 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).
Last edited by emurf; Dec 7th, 2007 at 9:58 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC