文件处理问题 [英] File handling question

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

问题描述

大家好,


假设给出了一个文本文件,我们必须编写一个程序,它将从文件中返回一个随机行。这很容易做到。但是如果文本文件太大而且不能完全适合主内存

那么

呢?在这种情况下,我们将如何修改我们的代码?


此外,如果我们获得流而不是文件,那么需要更改




谢谢,

Anunay

Hi all,

Suppose a text file is given and we have to write a program that will
return a random line from the file. This can be easily done. But what
if the text file is too big and can''t fit into the main memory
completely? In this case, how will we modify our code?

Also, if we are given a stream, instead of a file, then what changes
are required?

Thanks,
Anunay

推荐答案

Anunay说:
Anunay said:
大家好,

假设有一个文本文件,我们必须编写一个程序,它将从中返回一个随机行文件。这很容易做到。但是,如果文本文件太大而且不能完全适合主存储器呢?在这种情况下,我们将如何修改我们的代码?


完全没有,如果你第一次正确写的话。您需要存储两行:
两行:当前已保存的行。行和最近读取的行。

当您将行N读入内存时,将其复制到已保存行中。缓冲区

概率1 / N.在该过程结束时,保存的缓冲区将

包含一个随机选择的行。

另外,如果给我们一个流而不是文件,那么需要进行哪些更改?
Hi all,

Suppose a text file is given and we have to write a program that will
return a random line from the file. This can be easily done. But what
if the text file is too big and can''t fit into the main memory
completely? In this case, how will we modify our code?
Not at all, if you write it properly first time. You just need storage for
two lines: the currently "saved" line, and the most recently read line.
When you read line N into memory, copy it into the "saved" buffer with
probability 1/N. At the end of this process, the "saved" buffer will
contain a randomly selected line.
Also, if we are given a stream, instead of a file, then what changes
are required?




完全没有,如果您编写第一个程序的方式是在没有命令行参数的情况下从stdin获取输入

。< br $>

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



None at all, if the way you wrote the first program was to take the input
from stdin if no command line argument was forthcoming.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


" Anunay" <一个********* @ gmail.com>写道:
"Anunay" <an*********@gmail.com> writes:
大家好,

假设给出了一个文本文件,我们必须编写一个程序,它将从中返回一个随机行文件。这很容易做到。但是,如果文本文件太大而且不能完全适合主存储器呢?
这是什么问题?你逐行阅读文件,直到你的随机数为



在这种情况下,我们将如何修改我们的代码?
我不需要更改一行代码。
另外,如果给我们一个流而不是文件,那么需要进行哪些更改?
Hi all,

Suppose a text file is given and we have to write a program that will
return a random line from the file. This can be easily done. But what
if the text file is too big and can''t fit into the main memory
completely? What it the problem? You read the file line by line till you are at
your random number.
In this case, how will we modify our code? I do not have to change a line of code.
Also, if we are given a stream, instead of a file, then what changes
are required?


这是什么意思?


弗里德里希

-

请删除刚才的新闻 - 通过电子邮件回复。


What do you mean with this?

Friedrich
--
Please remove just-for-news- to reply via e-mail.


Friedrich Dominicus< ju ***************** @ q-software- solutions.de>写道:
Friedrich Dominicus <ju*****************@q-software-solutions.de> wrote:
" Anunay" <一个********* @ gmail.com>写道:
"Anunay" <an*********@gmail.com> writes:
大家好,

假设给出了一个文本文件,我们必须编写一个程序,它将从中返回一个随机行文件。这很容易做到。但是,如果文本文件太大而且不能完全适合主存储器呢?这有什么问题?你逐行阅读文件,直到你的随机数为止。
Hi all,

Suppose a text file is given and we have to write a program that will
return a random line from the file. This can be easily done. But what
if the text file is too big and can''t fit into the main memory
completely? What it the problem? You read the file line by line till you are at
your random number.




需要2次文件读取:一次计算行数,

选择一条随机线。这可以在读取文件时使用

(尽管不是最佳的,当然),但是对于流不可能。



Requiring 2 passes of file reading: one to count the number of lines,
one to choos a random line. This can be used when reading a file
(although not optimal, ofcourse), but is not possible with streams.

另外,如果给我们一个流而不是文件,那么需要进行哪些更改?
Also, if we are given a stream, instead of a file, then what changes
are required?



你对此有何看法?


What do you mean with this?




我认为OP意味着从标准输入而不是文件读取。


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



I assume the OP means reading from standard input, instead of a file.

--
:wq
^X^Cy^K^X^C^C^C^C


这篇关于文件处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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