Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Load an Array from a Text File (VB6)

    I am trying to load a single column text file (strings) into an Array. The Array has 6870 elements that I then want to sort into alphabetical order using a bubble sort. I have written a piece of test code to see that I am accessing the file correctly and this works okay. But I can't seem to get it to load into the Array.

    Code for the loading:

    General Declarations
    Const cMin = 0
    Const cMax = 6869
    Dim Dictionary(cMin To cMax) As String

    Form_Load
    Dim s As String
    Dim i As Integer

    For i = cMin To cMax
    Dictionary(i) = 0
    Do While (txtStream.AtEndOfStream = False)
    s = txtStream.ReadLine
    Loop
    Next i

    This compiles okay but my test :

    lblResult.Caption = Dictionary(10)

    always returns 0

    So where am I going wrong please?

    p.s. I know I probably should be using a dynamic array for this, and something more sophisticated than a bubble sort. Lack of skill issues, I'm afraid.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Load an Array from a Text File (VB6)

    1. Your code snippet doesn't make it clear if and how txtStream is opened, so it is impossible to give advice about the code.

    2. For 6,870 elements you really should use a better sort method than bubble sort. There are many ready-to-use code samples for shellsort and quicksort to be found on the net. There are some here in the Lounge, e.g. <post#=135077>post 135077</post#>.

    3. Why don't you open an ADO recordset on the text file and sort that?

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Load an Array from a Text File (VB6)

    Maybe you posted the wrong version, but you are setting Dictionary(i) = 0 in your loop.

    You might be able to use VB's Split method and the TextStream object's .ReadAll method to jam the entire file into an array in one go. But that might exceed the capacity of VB, I've never tried with anything that large.

    If you are interested in exploring ADO, which can treat your text file like a database, a search on this board might help. The most relevant threads used ADO to open a CSV-format file. If your strings are not enclosed in quotation marks, and particularly if they contain commas, you might need to set an option somewhere to treat the file as "tab-delimited." Unfortunately I can't remember how to do that...

  4. #4
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Load an Array from a Text File (VB6)

    Hi

    Thanks for all your help

    To reply to Hans first:

    1. Other half of the code

    General declarations
    Dim File As New FileSystemObject
    Dim Retroword As File
    Dim txtStream As TextStream

    Form_Load
    'Get the File
    Set Retroword = File.GetFile("F:retroword.txt")

    'Open Text stream for reading from the file
    Set txtStream = Retroword.OpenAsTextStream(ForReading)

    'Display path in lblFileName
    lblFileName.Caption = Retroword.Path

    This bit seems to work okay. The file path is displayed. And I have successfully searched the file.

    2. Yes. Will certainly investigate different sort methods.

    3. I know absolutely nothing about ADO record sets. There is not a lot on this in my text books. Although I haven't read everything, what there is seems to relate to manipulating data grids in MSAccess. The more simply and faster this list can be sorted the better. If ADO will do the job - brilliant. The actual specification was to "read in the file from source". Would ADO fulfil this? My strings are just a list. They are not contained in quotation marks. There are no commas.

    To reply to jscher2000

    Yes, I wondered about this. I have only used integer arrays before. I know something is happening because if I make
    lblResult.Caption = s
    then I get the last row of the text stream displayed.

    Duffy

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Load an Array from a Text File (VB6)

    Replace

    For i = cMin To cMax
    Dictionary(i) = 0
    Do While (txtStream.AtEndOfStream = False)
    s = txtStream.ReadLine
    Loop
    Next i

    with

    i = cMin
    Do While (txtStream.AtEndOfStream = False)
    Dictionary(i) = txtStream.ReadLine
    i = i + 1
    Loop

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Load an Array from a Text File (VB6)

    Hans code is going to get an error if there are more than cMax records in the file. You might want to modify his code to:

    <pre>i = cMin
    Do While (txtStream.AtEndOfStream = False)
    Dictionary(i) = txtStream.ReadLine
    i = i + 1
    If i > cMax Then Exit Do
    Loop
    </pre>

    Legare Coleman

  7. #7
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Load an Array from a Text File (VB6)

    Hi

    Thanks very much. Works brilliantly.

    Duffy

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Load an Array from a Text File (VB6)

    > I've never tried with anything that large

    One of my libraries is a 23MB string. I load it into memory (along wioth other libraries) and search it with Instr, and then extract segments with Mid$.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •