| | |
Help for file handling in VB. NET
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
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
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
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Solved Threads: 4
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.
VB.NET Syntax (Toggle Plain Text)
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
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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.
VB.NET Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- input from user from .txt (VB.NET)
- VC++ .net Build problem (C++)
- Mail to a list of people (PHP)
- Vb.net save html to file (ASP.NET)
- setting the web.config file (ASP.NET)
- Java Image Handling (Java)
- Escape and Unescape / Handling (C)
- "cannot resolve symbol"problem (Java)
- CSRSS Backspace Bug in Windows NT 4/NT 2000/NT XP (Windows NT / 2000 / XP)
Other Threads in the VB.NET Forum
- Previous Thread: Closing file in VB.NET
- Next Thread: Send Email with VS 2005 VB.net
| Thread Tools | Search this Thread |
.net 2005 2008 access account arithmetic array basic bing button buttons c# center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog folder ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project remove save searchbox searchvb.net select serial server shutdown soap survey table tcp temperature text textbox timer toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf





