在c ++中读取大的txt文件 [英] Read large txt file in c++
本文介绍了在c ++中读取大的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在内存中读取大约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))//总是将读取放在while条件中。
{//如果有数据到
,则只进入循环//使用myStr数据//进程。否则,您需要阅读,然后
} //测试读取是否正确
//
//注意:读取的最后一行将读取(但不是
/ /过去)然后结束的文件。因此,当
//文件中没有数据时,它的状态仍然是
// OK。直到你试图显式地
//读过EOF标志被设置的文件末尾。
为什么不显式调用close请参阅:
https://codereview.stackexchange.com/questions/540/如果效率是你的主要目标(可能不是)。如果效率是你的主要目标(可能不是)。然后将整个文件读入内存并从那里解析:请参阅下面的Thomas:用c ++读取大的txt文件
I'd like to read a file of about 5MB in memory ... the file has this format (it is a text file)
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
.....
how can I do this efficiently in c++?
解决方案
Reading a file line by line:
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.
For a reason to not explicitly call close see:
https://codereview.stackexchange.com/questions/540/my-c-code-involving-an-fstream-failed-review/544#544
If efficiency is your major goal (its probably not). Then read the whole file into memory and parse from there: see Thomas below: Read large txt file in c++
这篇关于在c ++中读取大的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文