Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wanna read lines from a text file randomly until all the lines are read. can anyone please think a piece of code to do that in vb.net?? thanks...!
Posted

See THIS[^], Accepted answer in that question will help you bit towards.
 
Share this answer
 
Strictly speaking, you can never read them randomly.

Wait! Before getting angry and down-voting, read thoroughly!

It depends what you call "read" and "randomly". Let's assume that you're allowed to each line only once (no pre-reading; if you allow pre-reading, the problem is trivial: read whole file into collection of lines and feed the lined at random :) ). Let's assume "randomly" means that random number generator generates a line number, then the algorithm reads and return the line of this number.

There is a well-known terminology: random access (like in RAM -- Random Access Memory).
If you mean something else, please skip the rest of my post and explain what exactly is require -- I'm not responsible for answering wrong question only because of your incomplete specification.

So, under these assumption, this is not possible with text file and lines where lines are separated by end of line characters. It is only possible (and trivial) if the file consist of fixed-size records, more exactly, if the file positions of each record boundaries are known in advance. Here is why:

Arbitrary text file is composed of lines of different length; lines are separated by some end-of-line characters which positions are unknown. In other words, there is no descriptor of the line lengths/positions which you could read at the end of the file. If you read sequentially, you discover where the lines are as you read. You can read randomly, but you go the the line you need to know its position -- and it is never known before you read everything before. Reading the content before a given line is equivalent to sequential reading => your reading is not random.
 
Share this answer
 
Comments
Espen Harlinn 11-Jan-11 11:02am    
5+ Good answer
Well according to your description, you may read some lines multiple times while others only once in a while. In true sense, the random number you will generate will not be evenly distributed, thus some will cause duplicate reads.

How about this, first figure our how many lines the text file has. This can be easily achieved. Say your get x-lines. Then you create a list of randomized list of x numbers (think of opposed of sorting). Now you go head and use this list to read each line from the file. This will guaranty each line is read only once and all lines will get read.

For efficiency, you can real all the lines into list first and then access the index of the list from the randomized list.

just my 2cents.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 13:45pm    
Yusuf, you understand things well, but this is not quite relevant. I can't see a requirement for for uniform distribution (that must be another stupid school assignment). I already pointed out it requires pre-reading, but this is not true random access.

...a boring discussion anyway. It can be bad assignment, inability to understand the assignment, etc.

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900