|
-
Oct 21st, 2002, 04:36 AM
#1
Thread Starter
Lively Member
How to write a file in VB6.0 and read it in VB.net
Hello.
I need to write a list of Longs to a file in an application written in VB6.0, and then read the same file through an application written in VB.net.
Writing the file in VB6.0 I can do as follows:
VB Code:
Dim fs As Integer
fs = FreeFile ' Get unused filenumber.
Open strFile For Binary Access Write As #fs
Put #fs, , lng_Data
Close #fs
..where lng_Data is an array of longs
But how do I read this array back into memory in VB.net?
Using a binary streamreader.. I am only allowed to read byte-arrays.
In VB.net, I could serialize/deserialize the array, but that is not compatible with VB6.0.
Ideas?
best reg.
BIW
-
Oct 21st, 2002, 11:07 AM
#2
Hyperactive Member
Use a text file so the autoconvert will do the work for you (open using "input" and "output" instead of "binary" and use "input #" and "print #" instead of "put" and "get"
-
Oct 21st, 2002, 11:11 AM
#3
I wonder how many charact
Using a binary streamreader.. I am only allowed to read byte-arrays.
A long is 4 bytes...(in vb6)...
So, just convert the byte array read from the Binaryreader into its numeric equivalent... using a byte to long or byte to int
function or code snippet... (you'll have to search for these)
Just remember an array of longs written in vb6 is equal to an array of Integers(Int32) in VB.Net
Last edited by nemaroller; Oct 21st, 2002 at 11:19 AM.
-
Oct 21st, 2002, 12:38 PM
#4
I wonder how many charact
Here is some code I whipped up you can use to read in the file... change the filename used to whatever you want... make sure to use
Imports System.IO
in your declerations section...
Tell me if its good enuf for you..
VB Code:
Dim filelength As FileInfo
Dim MyfileLength As Int64
Dim MyfileInfo= New FileInfo("C:\testfile.tst") 'get the filelength
MyfileLength = MyfileInfo.Length() / 4
'each long in vb6 is 4 bytes, so length of file / 4 is how many
'longs were stored
'loop counter
Dim i As Int64
'create a binaryreader to the file
Dim BR As New IO.BinaryReader(IO.File.OpenRead("C:\testfile.tst"))
'create a int32 (which is = to a vb6 long) array
Dim h() As Int32
'redimension to size of file elements
ReDim h(MyfileLength)
For i = 1 To MyfileLength
'.Readint32, reads 4 bytes, and advances the file pointer
h(i) = BR.ReadInt32
'write to debug window to show it works
Debug.WriteLine(CStr(h(i)))
Next
Last edited by nemaroller; Oct 21st, 2002 at 12:50 PM.
-
Oct 21st, 2002, 01:49 PM
#5
Thread Starter
Lively Member
Thanks for the response..
The problem with your suggestion Nemaroller.. is that reading just one value at the time is just too slow when reading up from files >100kB.
I'd like a solution where you could read the whole array in one go.
In VB6.0, I can read back the whole array with one Get #.
In VB.net I can read/write in one go by serializing the array.
But how do I write to the file with VB6.0 and read in one go with VB.net?
I did come up with one solution, where I code my data as a string.
Strings I can read/write in one go, from both languages. It messes up my code a little bit though.
best reg
BIW
-
Oct 21st, 2002, 02:09 PM
#6
I wonder how many charact
Obviously my code runs slow because on each looped read
1) its converting a Long into a String
2) writing that value to the debug window
If you comment out the debug.writeline method, it will run tremendously faster... I put that line in so you could see it works...
However, you can read all the bytes at once... using .ReadBytes method of the BinaryReader class.... and skip the for loop.
But, you will now have a byte array of bytes, where each 4 bytes represents one Long.... so you then have to feed that byte array into a function which converts it... like I said before, you can check the Internet using google for a function to do that.. or you can write it yourself...but it will undoubtedly be slower than ReadInt32 method of the BinaryReader
VB Code:
Dim filelength As FileInfo
Dim r As Int64
filelength = New FileInfo("C:\testfile.tst") 'get the filelength
r = filelength.Length
'each long in vb6 is 4 bytes, so length of file / 4 is how many
'longs were stored
'loop counter
Dim i As Integer
'create a binaryreader to the file
Dim BR As New IO.BinaryReader(IO.File.OpenRead("C:\testfile.tst"))
'create a int32 (comparable to vb6 long) array
Dim h() As Byte
'redimension to size of file elements
ReDim h(r)
h = BR.ReadBytes(r)
Debug.Write(CStr(r))
MsgBox("done")
Last edited by nemaroller; Oct 21st, 2002 at 02:45 PM.
-
Oct 21st, 2002, 02:36 PM
#7
I wonder how many charact
I tested my first code I posted with the debug.writeline commented out, and it read a 7mb file within 1 second...
So, just comment out the debug.writeline....
I also tested it with a 76mb file, and it read it in faster than VB6 wrote it out using the method you first described... (<5 secs)
Last edited by nemaroller; Oct 21st, 2002 at 02:40 PM.
-
Oct 21st, 2002, 04:11 PM
#8
Thread Starter
Lively Member
ok.. thanks!
I did something similar to what you have proposed.. and that code did not perform very well.
I do not have the code here, but it took several seconds reading in from a file containing about 100Kb. Must have done something wrong then.
I'll test your code out as soon as I get back to work.
best reg
BIW
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|