读档向后(最后一行先) [英] read file backwards (last line first)

查看:146
本文介绍了读档向后(最后一行先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件看起来是这样的:

ABCD

  EFGH

  IJKL

abcd
efgh
ijkl

我想用读取文件 C ,以便它第一次读到最后一行:

I want to read the file using C so that it read the last line first:

IJKL

  EFGH

  ABCD

ijkl
efgh
abcd

我似乎无法找到不使用阵列存储的解决方案。请帮助。

I cannot seem to find a solution that does not use an array for storage. Please help.

edit0:
感谢所有的答案。只是为了让你知道,我是一个创建这个文件。所以,我可以在某种程度上其以相反的顺序创建?这可能吗?

edit0: Thanks for all the answers. Just to let you know, I am the one creating this file. So, can I create in a way its in the reverse order? Is that possible?

推荐答案

它是这样的:


  1. 寻求到一个字节使用该文件的末尾 fseek的 。有没有保证,最后一行将有EOL所以最后一个字节并不重要。

  2. 阅读一个字节使用 龟etc

  3. 如果该字节是EOL则最后一行是一个空行,你拥有它。

  4. 再次
  5. 使用 fseek的往回走两个字节,并检查与字节龟etc

  6. 重复上面,直到你找到一个EOL。当你有一个EOL,文件指针将在接下来的(从端)行的开头。

  7. ...

  8. 利润。

  1. Seek to one byte before the end of the file using fseek. There's no guarantee that the last line will have an EOL so the last byte doesn't really matter.
  2. Read one byte using fgetc.
  3. If that byte is an EOL then the last line is a single empty line and you have it.
  4. Use fseek again to go backwards two bytes and check that byte with fgetc.
  5. Repeat the above until you find an EOL. When you have an EOL, the file pointer will be at the beginning of the next (from the end) line.
  6. ...
  7. Profit.

基本上,你必须继续做(4)和(5),同时保持跟踪你在哪里,当你发现一个行的开头,这样就可以开始您的扫描下一行开始前寻求回到那里的。

Basically you have keep doing (4) and (5) while keeping track of where you were when you found the beginning of a line so that you can seek back there before starting your scan for the beginning of the next line.

只要你在文本模式下打开您的文件,你不应该担心多字节EOLS在Windows(感谢您的提醒卢茨先生)。

As long as you open your file in text mode you shouldn't have have to worry about multibyte EOLs on Windows (thanks for the reminder Mr. Lutz).

如果你碰巧得到一个非可查找输入(如管道),那么你的运气,除非你想把你输入第一个转储到一个临时文件。

If you happen to be given a non-seekable input (such as a pipe), then you're out of luck unless you want to dump your input to a temporary file first.

所以,你可以做到这一点,但它是相当难看。

So you can do it but it is rather ugly.

您可以做pretty多使用同样的事情 MMAP ,如果​​你有 MMAP 可用的指针。该技术将是pretty大同小异:开始时结束,往后走,找到previous行的末尾

You could do pretty much the same thing using mmap and an pointer if you have mmap available. The technique would be pretty much the same: start at the end and go backwards to find the end of the previous line.

回复:?我是一个创建这个文件,所以,我可以在某种程度上打造以相反的顺序是可能的

您会遇到同样类型的问题,但他们会更糟糕。在C文件是从头开始,去到最后的字节顺序固有的名单;你想对这个基本财产工作,逆着基本面永远乐趣。

You'll run into the same sorts of problems but they'll be worse. Files in C are inherently sequential lists of bytes that start at the beginning and go to the end; you're trying to work against this fundamental property and going against the fundamentals is never fun.

你真的需要在纯文本文件中的数据?也许你需要text / plain的作为最终输出,但一路过关斩将?你可以存储在索引的二进制文件中的数据(甚至可能SQLite数据库),然后你只需要担心保持(或窗口),在内存中的索引,这就是不太可能是一个问题(如果是这样,使用一个真正的数据库);那么,当你把所有的线条,只是扭转指数和远离你去。

Do you really need your data in a plain text file? Maybe you need text/plain as the final output but all the way through? You could store the data in an indexed binary file (possibly even an SQLite database) and then you'd only have to worry about keeping (or windowing) the index in memory and that's unlikely to be a problem (and if it is, use a "real" database); then, when you have all your lines, just reverse the index and away you go.

这篇关于读档向后(最后一行先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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