[英]Read large txt file in c++
我想讀取內存中大約5MB的文件...該文件具有這種格式(它是一個文本文件)
ID 3: 0 itemId.1 0 itemId.2 0 itemId.5 1 itemId.7 ........................ 20 itemId.500
ID 50: 0 itemId.31 0 itemId.2 0 itemId.4 2 itemId.70 ........................ 20 itemId.2120
.....
如何在C ++中有效地做到這一點?
逐行讀取文件:
ifstream fin ("file.txt");
string myStr;
while(getline(fin, myStr)) // Always put the read in the while condition.
{ // Then you only enter the loop if there is data to
//use myStr data // processes. Otherwise you need to read and then
} // test if the read was OK
//
// Note: The last line read will read up to (but not
// past) then end of file. Thus When there is
// no data left in the file its state is still
// OK. It is not until you try and explicitly
// read past the end of file that EOF flag is set.
由於未明確調用close的原因,請參見:
https://codereview.stackexchange.com/questions/540/my-c-code-involving-an-fstream-failed-review/544#544
如果效率是您的主要目標(可能不是)。 然后將整個文件讀入內存並從那里進行解析:請參見下面的Thomas: 用c ++讀取大型txt文件
將整個文件讀入內存,然后處理內存中的內容。
電動機保持旋轉狀態時,文件資源(例如,硬盤驅動器)效率最高。 因此,一次大數據讀取要比5次小數據讀取更有效率。
在大多數平台上,內存訪問比文件訪問更快。 使用此信息,可以通過將數據讀入內存然后處理內存來提高程序的效率。
將這兩種技術結合在一起將產生更高的性能:一次交易將盡可能多的數據讀入內存,然后處理內存。
有些人聲明了char
或unsigned char
(用於二進制數據)的大型數組。 其他人告訴std :: string或std :: vector保留大量內存,然后將數據讀入數據結構。
另外,塊讀取(也稱為istream::read()
)將繞過C ++流功能的大多數慢速部分。
使用文件流 :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline(myfile, line) )
cout << line << endl;
myfile.close();
}
else
{
cout << "Unable to open file";
}
return 0;
}
5MB確實不是一個大文件。 流將為您一次讀取大塊數據,但實際上是這樣; 幾乎所有在其上運行的計算機都將能夠將5MB的數據正確地讀入內存中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.