Results 1 to 10 of 10

Thread: Random access lines in a *.txt file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Random access lines in a *.txt file

    Hello, this is my first post and I am a new member to the VB world, and really new to the .net family. Okay I have a project that requires me to access a *.txt file that contains a list of names and randomly pick three of the names and display them out. I have a limited program right now that pulls in only 1 name from the list cant get it to read past the space, and havent started on the random part yet. Any input will help, and thank you for your time.

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Random access lines in a *.txt file

    Hi,

    Welcome to the Forums.

    One way of doing what you require would be to choose 3 random numbers, pick the largest and read in that many lines of data using a StreamReader, then just use the lines corresponding to the numbers you picked.

    For Random Number Generation, investigate:

    Randomize
    Rnd

    There are plenty of examples on MSDN.

    For StreamReader, try:

    StreamReader.ReadLine()


    Again, plenty of examples. I have one in front of me that will do pretty much what you want, with minor modification.
    You'll need a "For i = 1 to n" loop, where n is the largest of your 3 random numbers, to read in the first n lines of the txt file. You'll then need to ensure that "If i = randomnumber1 then strfirstline = strreadinfromfile".

    I hope that makes some sense. You'll find most people here to be very helpful in correcting code, less so when it comes to coding other people's work for them. So have a crack at it, and then if and when it doesn't work it'll get amended. You'll also learn a lot more that way.

    HTH

    zaza

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Random access lines in a *.txt file

    This maybe sloppy, as I'm doing it from memory.
    VB Code:
    1. Imports System.Text.RegularExpressions
    2. ...
    3. 'Each line of data should now be a unique value in the array
    4. Dim Names() as String = _
    5. Regex.Split(IO.File.OpenText("C:\namefile.txt").ReadToEnd(),"\r\n", RegexOptions.SingleLine)
    6. For i as Int32 = 0 to 2
    7.      'Call your random number generator
    8.      Dim iRnd as Int32 = RandomNumber(0,Names.Length - 1)
    9.      'Process Names(iRnd)
    10. Next

  4. #4
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Random access lines in a *.txt file

    Hey...
    Instead of trying so hard to access lines in a .txt file,you could store them in an easy way so that theyre easy to retrieve.
    I have made an example to show you how to store onto a file,so that you can easily retrieve it back...but,I hope you wont get confused seeing this...Ive put the actual code in reverse,so that you can understand how it works...if you can understand this,tell me and then Ill tell you how to retrieve the data back. ...I have not posted the structure for pat1.Just see the index first.You dont need to understand how the savefile function works.Just see the other part.Im declaring everything as a structure and saving it so that it would be easy to retrieve..
    VB Code:
    1. Public Function savefile()
    2.         Prepareindex()
    3.         preparesave()
    4.  
    5.         lastrecord = FindLastindexRecordNo()
    6.         filenum = FreeFile()
    7.         FileOpen(1, Application.StartupPath & "\example.txt", OpenMode.Random, , , Len(pat1))
    8.         FilePut(1, pat1, )
    9.         FileClose(1)
    10.         'indexfile
    11.         FileOpen(filenum, Application.StartupPath & "\index.txt", OpenMode.Random, , , Len(ind))
    12.         FilePut(filenum, ind, lastrecord)
    13.         FileClose(filenum)
    14.     End Function
    15.  
    16.  Public Function preparesave()
    17.         pat1.name = frm1.textBox1.Text
    18.         pat1.address=frm1.textbox2.text
    19.     End Function
    20.  
    21.  Public Function Prepareindex()
    22.         ind.myid = frm1.textBox1.Text
    23.         ind.myname = frm1.txtname.Text
    24.         ind.myaddress = frm1.txtrdname.Text
    25.  
    26.     End Function
    27.  
    28. Public Structure index
    29.         <VBFixedString(15)> Dim myid As String
    30.         <VBFixedString(30)> Dim myname As String
    31.         <VBFixedString(30)> Dim myaddress As String
    32.       End Structure
    33.  
    34. Dim ind as index

    Godwin

    P.S:If I am confusing you,please forget this idea

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Random access lines in a *.txt file

    Randomize and Rnd are classic VB, not .NET. The Random object replaces those two, and is considerably more usefull in my opinion.

    If you will be doing this more than once, it will be faster to read the whole file into an array right off. Then multiple names can be picked easily. In fact, since you are picking three names, this is probably the way to go anyways.

    Alternatively, you could get your three random numbers, and read in names up to the highest number. That would save you the time to read that portion of the file which you do not need. In general, you can't dip into a text file and pull out a specific thing, so true random access isn't really available. It can be done, but there is a cost involved that is often not worthwhile.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Random access lines in a *.txt file

    Hey Shaggy..
    I dont understand what you mean exactly..
    Is it possible for you to show me the code if possible?
    I use this way to use storing on files as an alternative to database.After storing it this way,I encrypt them...if you know some other way,please tell me

    Godwin

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Re: Random access lines in a *.txt file

    hey guys thanks for all the input, i have been working on this for awhile now and i have some real inefficient code, what i ended up doing was makin a random # and then a case statement to pick a diffrent txt file, and output that list of names (which only contains 3 names per file). Is there a way to output a specific line of text from a file and randomize that number.
    ex.
    something like
    line = randomnumber.Next(10)
    Console.WriteLine(line("test.txt"))

    i know this is not remotely correct code, like i said very new to the vb & .net
    world sorry!!

    thanks again for all the help

  8. #8
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Random access lines in a *.txt file

    YOu could also use a collection with the streamreader in a simple way:

    VB Code:
    1. Private Sub PickThree()
    2.          Dim TextLineCol as new Collection
    3.          Dim sr As New IO.StreamReader(Application.StartupPath + "\textfile.txt")
    4.         Dim line As String
    5.         Do
    6.             line = sr.ReadLine()
    7.             TextLineCol.Add(line)
    8.         Loop Until line Is Nothing
    9.         Dim newrnd As New Random
    10.         MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
    11.         MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
    12.         MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
    13.     End Sub

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    3

    Re: Random access lines in a *.txt file

    Thanks alot that seemed to help alot, what exactly does collection do,
    havent seen that one before. Well thanks again for all the help.

  10. #10
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Random access lines in a *.txt file

    Collections are *disclaimer* kind of like arrays. Only, they are far easier than arrays to manage dynamically. Also, you can add all different types of objects to a collection, not just one kind of variable.

    Bill

    Edit - Also, to confuse us completely, collection members start numbering at 1 instead of zero like everything else.
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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