Results 1 to 8 of 8

Thread: How to write a file in VB6.0 and read it in VB.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74

    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:
    1. Dim fs As Integer
    2.            
    3.             fs = FreeFile    ' Get unused filenumber.
    4.             Open strFile For Binary Access Write As #fs
    5.                                                                        
    6.             Put #fs, , lng_Data
    7.            
    8.             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

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    Toronto, Ontario
    Posts
    280
    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"

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Dim filelength As FileInfo
    2.         Dim MyfileLength As Int64
    3.         Dim MyfileInfo= New FileInfo("C:\testfile.tst") 'get the filelength
    4.         MyfileLength = MyfileInfo.Length() / 4
    5.         'each long in vb6 is 4 bytes, so length of file / 4 is how many
    6.         'longs were stored
    7.  
    8.         'loop counter
    9.         Dim i As Int64
    10.  
    11.         'create a binaryreader to the file
    12.         Dim BR As New IO.BinaryReader(IO.File.OpenRead("C:\testfile.tst"))
    13.         'create a int32 (which is = to a vb6 long) array
    14.         Dim h() As Int32
    15.         'redimension to size of file elements
    16.         ReDim h(MyfileLength)
    17.  
    18.  
    19.         For i = 1 To MyfileLength
    20.             '.Readint32, reads 4 bytes, and advances the file pointer
    21.             h(i) = BR.ReadInt32
    22.             'write to debug window to show it works
    23.             Debug.WriteLine(CStr(h(i)))
    24.         Next
    Last edited by nemaroller; Oct 21st, 2002 at 12:50 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74
    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

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Dim filelength As FileInfo
    2.         Dim r As Int64
    3.         filelength = New FileInfo("C:\testfile.tst") 'get the filelength
    4.         r = filelength.Length
    5.         'each long in vb6 is 4 bytes, so length of file / 4 is how many
    6.         'longs were stored
    7.  
    8.         'loop counter
    9.         Dim i As Integer
    10.  
    11.         'create a binaryreader to the file
    12.         Dim BR As New IO.BinaryReader(IO.File.OpenRead("C:\testfile.tst"))
    13.         'create a int32 (comparable to vb6 long) array
    14.         Dim h() As Byte
    15.         'redimension to size of file elements
    16.         ReDim h(r)
    17.  
    18.         h = BR.ReadBytes(r)
    19.         Debug.Write(CStr(r))
    20.         MsgBox("done")
    Last edited by nemaroller; Oct 21st, 2002 at 02:45 PM.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    74
    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
  •  



Click Here to Expand Forum to Full Width