我有一個大文件,我只需要從它得到最後一行(\n
只是行分隔符)。
我需要這個在iOS設備上完成,所以它不會佔用太多的內存或CPU時間(如閱讀整個文件)。
如何在Objective-C,C++或C++ 11中執行此操作?讀取文件的最後一行
4
A
回答
3
我在我的生產代碼中有這個功能。想法是試着通過查找和閱讀來閱讀最後一行。請看一看。
bool readLastLine(std::string const& filename, std::string& lastLine)
{
std::ifstream in(filename.c_str(),std::ifstream::binary);
if(!in) return false;
in.seekg(0, std::ifstream::end);
const std::streamoff len = in.tellg();
//empty file
if(len == 0)
{
lastLine = "";
return true;
}
int buf_size = 128;
std::vector<char> buf;
while(in)
{
if(buf_size > len)
{
buf_size = len;
}
buf.resize(buf_size);
in.seekg(0 - buf_size, std::ifstream::end);
in.read(&buf[0],buf_size);
//all content is in the buffer or we already have the complete last line
if(len == buf_size || std::count(buf.begin(), buf.end(), '\n') > 1)
{
break;
}
//try enlarge the buffer
buf_size *= 2;
}
//find the second line seperator from the end if any
auto i = std::find(++buf.rbegin(),buf.rend(), '\n');
lastLine.assign(i == buf.rend() ? buf.begin() : buf.begin() + std::distance(i, buf.rend()), buf.begin() + buf_size);
return true;
}
5
2
@Nerdtron答案似乎是最適合我,如果你沒有在你的文件格式的控制,但是...
如果你有在文件格式的控制,你可以做到這一點與O(1)複雜性。只需將文件開頭的最後一行的起始偏移量寫入(常量)偏移量中,然後再向其中寫入數據。當你想讀取它時,讀取這個偏移量,並轉到它指定的偏移量。
0
我想出了這個,試圖改善布魯斯,上面爲緩衝區並不需要調整,只是不停地從EOF以往更遠讀取字符的相同大小的塊:
std::string lastLine(std::ifstream &file)
{
if (!file.good()) throw exception("Bad stream on input");
const size_t bufSize = 80; // because why not? tweak if need to
char buf[bufSize];
string line;
int seek, nloff;
// iterate over multiples of bufSize while file ok
for (size_t n = 1; file; ++n)
{
// next seek position will be a multiple of bufSize
seek = -static_cast<int>(n * bufSize);
file.seekg(seek, file.end);
// read "bufSize" bytes into buffer
file.read(buf, bufSize);
// in case no newline found, seek past eof
nloff = -seek;
// find offset of last newline in buffer
for (size_t i = 0; i < bufSize; ++i)
{
if (buf[i] == '\n') nloff = i;
}
seek += nloff + 1; // new seek position is one character after found newline
if (seek >= 0) continue; // just kidding about the "past eof" part ;)
// seek to after found newline and get line
file.seekg(seek, file.end);
getline(file, line);
if (!line.empty()) break; // have result, break and return
}
if (file.good()) return line;
else return string();
}
相關問題
- 1. 從文件中讀取最後一行
- 2. 讀取文件的最後30,000行
- 3. 讀取文件中的第一行和最後一行
- 4. 讀取文件中最後一行的最快方法
- 5. 閱讀python文件的最後一行
- 6. 從文本文件中讀取第5行最後一行
- 7. 從文件中讀取最後N行
- 8. 如何高效地讀取文本文件的最後一行
- 9. 讀取文本文件的最後一行C#
- 10. 讀取gzip文本文件的最後一行
- 11. 一行一行地讀取文件,但是反過來(最後一行先,然後是最後一行等)
- 12. 如何讀取1GB的文件的最後一行 - java的
- 13. Perl只讀取我的文件的最後一行?
- 14. 逐行讀取文件只返回最後一行
- 15. [R readlines方法讀取文件的只有最後一行
- 16. 如何高效地讀取C++文件的最後一行
- 17. 在Java中使用openCSV讀取CSV文件的最後一行
- 18. 如何不讀取文件中的最後一行?
- 19. r讀取json文件不完整的最後一行發現
- 20. 從nxc中的文件讀取最後一行
- 21. 用C#讀取文件最後一行的高性能方法
- 22. 如何讀取最後一行在C#中的文本文件,第一行
- 23. 從讀行讀取最後一組文本,並更改文本?
- 24. 這是讀取Java文本文件最後一行的最佳方法嗎?
- 25. c +最快的方式來只讀取最後一行文本文件?
- 26. 使用StreamReader讀取文件的最後一行後執行操作
- 27. 在到達最後一行後,逐行讀取文件行while循環
- 28. 在FileSystemWatcher檢測到更改後讀取文本文件的最後一行
- 29. 在FileSystemWatcher檢測到更改後讀取文本文件的最後一行
- 30. 用python讀取csv文件讓我到最後一行
在這裏你有一個問題,你多次閱讀結尾,並且當它沒有壞道時多次查看結束行 - 你已經閱讀過,並且你知道它沒有新行。 – Dani
@Dani你可以做任何優化。對於大多數情況,只要最後一行的長度小於128,我們只讀取最後一行。我可以將默認緩衝區大小更改爲512.它適用於我的產品。 – BruceAdi