如何扭转订单行的文件文件

2023-09-11 23:28:28 作者:你瞎阿撞我心上来

如何才能敬畏订单行的文件不是自己行。 文件可以得到巨大的。

How can we revere order of lines in a file not the lines themselves. File can get huge.

没有假设应作出关于行的长度。

No assumption should be made about the length of a line.

输入:

this is line1
this is line2
this is line3

示例输出:

this is line3
this is line2
this is line1

不过,我觉得利用另一个文件作为缓冲,就像一个堆栈的数据结构,但不能真的用它去任何地方。

I though of making use of another file as buffer, like a stack data structures, but could not really go anywhere with it.

在这个有什么想法?

推荐答案

读取文件中的大块开始的两端。里面那些块,交换第一行的最后一行,然后将两个指针来跟踪你身在何处。写出每个块,你填充它。当两个指针在中间相遇,你​​做。

Read in large blocks of the file starting at both ends. Inside those blocks, swap the first line for the last line and then move both pointers to keep track of where you are. Write out each block as you fill it. When the two pointers meet in the middle, you are done.

不要试图修改到位块,这将使事情变得更加复杂。使用四大块,第一个读取的块,第一次写入的块,最后一次读取的块,最后写入块。由于每个写入区块完成后,写出来。由于每个读取块被耗尽,读一个又一个。要小心,不要覆盖任何东西你还没有看!

Don't try to modify the blocks in place, that will make things more complicated. Use four blocks, the first read block, the first write block, the last read block, and the last write block. As each write block is complete, write it out. As each read block is exhausted, read in another one. Be careful not to overwrite anything you've not yet read!

这应该是相当简单,只是单调乏味。如果你不需要它是最佳的,你可以只读取的块向后并写出一个新的文件,然后将它放在现有文件的顶部。

It should be fairly straightforward, just tedious. If you don't need it to be optimal, you can just read blocks backwards and write out a new file and then move it on top of the existing file.