CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    2

    Reading File, Loading to Array

    I am a VERY amatuer programmer (VB.NET 2003, Framework 2.0) who made an application that reads a text file, loads all the lines of that text file into an array, and randomly selects one line in that array to display it in a text box in the program.

    However, I have had some issues with this code segment:

    Code:
                        'Reading Race File for Length
                        ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
                        z1 = 0
                        Do Until ItemRead.Peek() = -1
                            monkey = ItemRead.ReadLine()
                            z1 = z1 + 1
                        Loop
                        ItemRead.Close()
                        
        'randomizing variable
                        Randomize()
                        Item = randomgen.Next(0, z1)
    
    
        'Reading Race File into array
                        ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
                        z2 = 0
                        Do Until z2 = z1
                            Itemlist(z2) = ItemRead.ReadLine()
                            z2 = z2 + 1
                        Loop
                        ItemRead.Close()
    The first thing this segment of code does is access the file, and read each line until the entire document is read through, assigning a number to the counter variable "z1" until the file is finished. The "monkey" variable does nothing whatsoever. I may remove it if it'll work without it.

    Next, it picks a random integer between 0 and z1, which will be the line number displayed.

    Lastly, it reads the file into an array called "Itemlist(z2)". I use z1 to define the limits of the array, but I get an out of bounds error when it tries to write the array. What am I missing?


    Also, while I'm at it, is there an easier way to say "load each line into part of an array, and randomly pick one of those lines"? I've been having array dimension errors since I started on this program.



    Also, I should note that when I have a file with 100 lines, this works without fail...but every time I try one with 120 lines, it crashes the application.

  2. #2
    Join Date
    May 2008
    Posts
    2

    Re: Reading File, Loading to Array

    ...D'OH! I'm an idiot.

    I worked for the longest time to fix this, and posted it here, but when I went to edit and post how I was dimensioning my variables, I realized the problem. It's fixed now.


    Still, I must wonder...is there a faster and better way to read a single random line of text from a text document of any length?



    Right now I'm counting each line, picking a line number from those results at random, loading the whole file to an array (using the line counter as the top limit), and calling that line's number from that array.
    Last edited by MM007; May 20th, 2008 at 10:21 AM.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading File, Loading to Array

    Sure. Look at the bottom sample:

    http://www.startvbdotnet.com/files/default.aspx
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Reading File, Loading to Array

    Quote Originally Posted by MM007
    ...
    Still, I must wonder...is there a faster and better way to read a single random line of text from a text document of any length?
    Add "Imports System.IO" as first line in the Module/Form

    to store each line in an array ArrayWithLines do:
    Code:
     			Dim fs As FileStream = File.OpenRead(Dir + "\Race\" + Race + ".txt")
    			Dim sr As New StreamReader(fs)
    			Dim ArrayWithLines() as String = Split(sr.ReadToEnd(), vbCrLf)
    			fs.Close()

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

Featured