简体   繁体   English

如何使用CStdioFile读取最后10行文本文件

[英]How to read last 10 line of text file using CStdioFile

I would like to read last 10 lines of a big text file. 我想阅读一篇大文本文件的最后10行。 Normally I loop through the file and get the last 10 lines. 通常我循环遍历文件并获取最后10行。 Please kindly tell me, are there other ways to read the last lines quicker. 请告诉我,还有其他方法可以更快地阅读最后一行。

Thanks. 谢谢。

You can get the file's length in bytes with ( CStdioFile::GetLength ) (inherited from CFile ): 您可以使用( CStdioFile::GetLength )(继承自CFile )获取文件的长度(以字节为单位):

http://msdn.microsoft.com/en-US/library/b569d0t4(v=VS.80).aspx http://msdn.microsoft.com/en-US/library/b569d0t4(v=VS.80).aspx

And you can use CStdioFile::Seek to jump to an arbitrary offset: 你可以使用CStdioFile::Seek跳转到任意偏移量:

http://msdn.microsoft.com/en-US/library/8c5ccz0x(v=VS.80).aspx http://msdn.microsoft.com/en-US/library/8c5ccz0x(v=VS.80).aspx

If the line length is fixed, it's a pretty easy problem to read the last N lines. 如果线长是固定的,那么读取最后N行是一个非常容易的问题。 But there's no "read backwards" operation, and in the general case you don't know how long the lines in an arbitrary text file are. 但是没有“向后读”操作,并且在一般情况下,您不知道任意文本文件中的行有多长。 (You could build and maintain an index file that kept track of it, if you wanted to do this faster.) (如果你想更快地做到这一点,你可以构建和维护一个跟踪它的索引文件。)

Be aware that seeking and reading one character at a time may be inefficient. 请注意,一次寻找和阅读一个角色可能效率低下。 Exactly how inefficient depends on several factors. 究竟效率如何低效取决于几个因素。 Although buffering at the operating system level might make it not as terrible as it could be if it went back to the disk each time, there's always going to be overhead each time you seek and each time you call a read operation. 虽然在操作系统级别进行缓冲可能会使它不像每次重新回到磁盘时那样糟糕,但每次寻找和每次调用读取操作时总会有开销。

So you would probably be better off picking a buffer size, and step through reading chunks. 所以你可能最好选择缓冲区大小,并逐步阅读块。 As a strong indication that this is useful, see some source code for the UNIX called tail . 作为一个有用的强烈迹象,请参阅UNIX的一些源代码,称为tail It gets the last N lines of a file, and although it's written in C it might give you some insight: 它获取文件的最后N行,虽然它是用C语言编写的,但它可能会给你一些见解:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c#n477 http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c#n477

In binary files, you (normally) have a fixed record length. 在二进制文件中,您(通常)具有固定的记录长度。 In text files, one possibility is to consider the text line as the record in which you divide the contents of the file. 在文本文件中,一种可能性是将文本行视为用于划分文件内容的记录。 Unfortunately, lines are not of fixed size in regular text files, so you cannot rely in a formula such as: 不幸的是,在常规文本文件中行不是固定大小的,因此您不能依赖公式,例如:

lastTenthLine = ( LengthOfFile / LengthOfLine ) - 10;

The only way I can think of is to move the reading pointer to the end of the file. 我能想到的唯一方法是将读指针移动到文件的末尾。 Tne start reading backwards, one char each time, and count the number of times you pass over a '\\n' char. 开始向后阅读,每次一个字符,并计算你通过'\\ n'字符的次数。 Once you have counted ten times, you can return the next char position. 计算十次后,您可以返回下一个焦点位置。

(Note that you can have "end line issues" depending on the origin (OS) of the files you are using as example. Since you are ussing MFC, you won't have any problem as long as the files used were generated in Windows) (请注意,根据您使用的文件的来源(OS),您可能会出现“结束行问题”。由于您正在使用MFC,只要在Windows中生成使用的文件,就不会有任何问题)

Since I suppose this is homework, I won't post code. 由于我认为这是作业,我不会发布代码。 You have documentation about MFC here, explaining how to deal with files. 您在这里有关于MFC的文档,解释了如何处理文件。 You need the CFile::Read and CFile::Seek methods (apart from Open and Close). 您需要CFile::ReadCFile::Seek方法(除了打开和关闭)。 Note that CFile is the parent class of CStdioClass . 请注意, CFileCStdioClass的父类。

http://msdn.microsoft.com/en-us/library/ey6xh9bk(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ey6xh9bk(v=vs.80).aspx

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM