随机读取文件中的行 [英] Reading lines from a file randomly

查看:54
本文介绍了随机读取文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本文件中随机读取行,直到读取所有行.任何人都可以在vb.net中考虑一段代码来做到这一点吗?谢谢...!

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...!

推荐答案

请参阅 ^ ],在该问题中接受的答案将有助于您朝着正确的方向前进.
See THIS[^], Accepted answer in that question will help you bit towards.


严格来说,您永远不能随机阅读它们.

等等!在生气和投下反对票之前,请仔细阅读!

这取决于您所说的阅读"和随机".假设您只允许每行一次(不进行预读;如果您允许预读,那么问题就微不足道了:将整个文件读入行的集合中,并随机输入行内的行:))) .假设随机"表示随机数生成器生成一个行号,然后算法读取并返回该数字的行.

有一个众所周知的术语:随机访问(例如在RAM中-随机访问内存).
如果您还有其他意思,请跳过我的其余文章,并解释确切的要求-仅由于您的规范不完整,我不负责回答错误的问题.

因此,在这些假设下,这对于文本文件和其中行由行尾字符分隔的行是不可能的.如果文件由固定大小的记录组成,更确切地说,如果每个记录边界的文件位置是事先已知的,则这是可能的(并且是琐碎的).原因如下:

任意文本文件由不同长度的行组成;行之间用一些行尾字符分隔,这些字符的位置未知.换句话说,在文件末尾没有可读取的行长度/位置的描述符.如果按顺序阅读,您会发现阅读的行在哪里.您可以随机阅读,但是您需要知道这条线的位置,因此在您阅读之前的所有内容之前,它永远是未知的.在给定的行之前读取内容等同于顺序读取=>.您的阅读不是随机的.
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.


根据您的描述,您可能多次阅读某些行,而另一些则偶尔阅读一次.从真正意义上讲,您将生成的随机数不会平均分布,因此某些随机数会导致重复读取.

怎么样,首先要弄清楚文本文件有多少行.这很容易实现.说您得到x线.然后,您将创建一个由x个数字组成的随机列表(请考虑反对排序).现在,您可以使用此列表从文件中读取每一行.这将保证每行只能读取一次,并且所有行都将被读取.

为了提高效率,您可以先将所有行都放入列表中,然后再从随机列表中访问列表的索引.

只是我的2美分.
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.


这篇关于随机读取文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆