是否有有效的 C 或 C++ 方法来读取 CSV 文件的最后一行?天真的方法是读入整个文件然后读到最后。有没有更快的方法可以完成此操作(特别是如果 CSV 文件很大)?
最佳答案
你可以做的是猜测行的长度,然后在文件末尾之前跳转 2-3 行并读取剩余的行。你读到的最后一行就是最后一行,只要你之前至少读过一行(否则,你仍然会以更大的偏移量重新开始)
我在 this answer 中发布了一些用于执行类似操作(读取最后 N 行)的示例代码(在 PHP 中,但作为说明)
有关各种语言的实现,请参阅
- C++:c++ fastest way to read only last line of text file?
- python :Efficiently finding the last line in a text file
- Perl:How can I read lines from the end of file in Perl?
- C#:Get last 10 lines of very large text file > 10GB c#
- PHP:how to read only 5 last line of the txt file
- Java:Read last n lines of a HUGE file
- ruby :Reading the last n lines of a file in Ruby?
- objective-c :How to read data from NSFileHandle line by line?
关于c++ - 高效读取csv文件的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762400/