|
-
Jul 30th, 2005, 04:24 PM
#1
Thread Starter
New Member
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.
-
Jul 30th, 2005, 04:41 PM
#2
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
-
Jul 30th, 2005, 05:29 PM
#3
Re: Random access lines in a *.txt file
This maybe sloppy, as I'm doing it from memory.
VB Code:
Imports System.Text.RegularExpressions
...
'Each line of data should now be a unique value in the array
Dim Names() as String = _
Regex.Split(IO.File.OpenText("C:\namefile.txt").ReadToEnd(),"\r\n", RegexOptions.SingleLine)
For i as Int32 = 0 to 2
'Call your random number generator
Dim iRnd as Int32 = RandomNumber(0,Names.Length - 1)
'Process Names(iRnd)
Next
-
Jul 30th, 2005, 10:19 PM
#4
Fanatic Member
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:
Public Function savefile()
Prepareindex()
preparesave()
lastrecord = FindLastindexRecordNo()
filenum = FreeFile()
FileOpen(1, Application.StartupPath & "\example.txt", OpenMode.Random, , , Len(pat1))
FilePut(1, pat1, )
FileClose(1)
'indexfile
FileOpen(filenum, Application.StartupPath & "\index.txt", OpenMode.Random, , , Len(ind))
FilePut(filenum, ind, lastrecord)
FileClose(filenum)
End Function
Public Function preparesave()
pat1.name = frm1.textBox1.Text
pat1.address=frm1.textbox2.text
End Function
Public Function Prepareindex()
ind.myid = frm1.textBox1.Text
ind.myname = frm1.txtname.Text
ind.myaddress = frm1.txtrdname.Text
End Function
Public Structure index
<VBFixedString(15)> Dim myid As String
<VBFixedString(30)> Dim myname As String
<VBFixedString(30)> Dim myaddress As String
End Structure
Dim ind as index
Godwin
P.S:If I am confusing you,please forget this idea
-
Jul 31st, 2005, 11:02 AM
#5
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
 
-
Jul 31st, 2005, 01:06 PM
#6
Fanatic Member
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
-
Jul 31st, 2005, 01:24 PM
#7
Thread Starter
New Member
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
-
Jul 31st, 2005, 11:32 PM
#8
Re: Random access lines in a *.txt file
YOu could also use a collection with the streamreader in a simple way:
VB Code:
Private Sub PickThree()
Dim TextLineCol as new Collection
Dim sr As New IO.StreamReader(Application.StartupPath + "\textfile.txt")
Dim line As String
Do
line = sr.ReadLine()
TextLineCol.Add(line)
Loop Until line Is Nothing
Dim newrnd As New Random
MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
MsgBox(TextLineCol(newrnd.Next(1, TextLineCol.Count)))
End Sub
Bill
-
Aug 1st, 2005, 12:54 PM
#9
Thread Starter
New Member
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.
-
Aug 1st, 2005, 02:27 PM
#10
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.
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
|