Help for file handling in VB. NET

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

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
  #11
Dec 7th, 2007
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
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
  #12
Dec 8th, 2007
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
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
  #13
Dec 10th, 2007
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.

  1. Dim s1 As FileStream 'Load file1
  2. Dim s2 As FileStream 'Load file2
  3. Dim s3 As FileStream 'Save output file
  4.  
  5. '---read from and write to a binary file
  6. s1 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
  7. s2 = New FileStream(openFD.FileName, FileMode.Open, FileAccess.Read)
  8. s3 = New FileStream(saveFD.FileName, FileMode.CreateNew, FileAccess.Write)
  9.  
  10.  
  11. Dim br1 As BinaryReader
  12. Dim br2 As BinaryReader
  13. Dim bw As BinaryWriter
  14.  
  15. br1 = New BinaryReader(s1)
  16. br2 = New BinaryReader(s2)
  17. bw = New BinaryWriter(s3)
  18.  
  19. Dim f1 As New System.IO.FileInfo(openFD.FileName)
  20. Dim intFileLen As Integer
  21. intFileLen = f1.Length
  22. Dim dblFiles(intFileLen, 1) As Double
  23. Dim bytFiles(intFileLen, 1) As Byte
  24. Dim intI As Integer, dblOut As Double
  25.  
  26. Try
  27. 'read the files
  28. For intI = 0 To intFileLen - 1
  29. 'get the vals & put into the array
  30. bytFiles(intI, 0) = br1.ReadByte
  31. bytFiles(intI, 1) = br2.ReadByte
  32. Next
  33.  
  34.  
  35. 'write the vals
  36. For intI = 0 To intFileLen - 1
  37. 'check the vals for division by 0
  38. If bytFiles(intI, 0) = 0 Or bytFiles(intI, 1) = 0 Then
  39. dblOut = 0
  40. Else
  41. dblOut = bytFiles(intI, 0) / bytFiles(intI, 1)
  42. End If
  43. 'write the val
  44. bw.Write(dblOut)
  45. Next
  46.  
  47. Catch ex As Exception
  48. Throw ex
  49. End Try
  50.  
  51. br1.Close()
  52. br2.Close()
  53. bw.Close()
  54.  
  55. Return dblOut
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: tathumani is an unknown quantity at this point 
Solved Threads: 0
tathumani tathumani is offline Offline
Newbie Poster

Re: Challenge in VB

 
0
  #14
Jun 16th, 2009
Originally Posted by emurf View Post
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).



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