Click here to Skip to main content
13,045,379 members (123,223 online)
Rate this:
Please Sign up or sign in to vote.
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 thanks...!
Posted 10-Jan-11 18:17pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See THIS[^], Accepted answer in that question will help you bit towards.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Espen Harlinn 11-Jan-11 11:02am
5+ Good answer
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
SAKryukov 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Jan 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100