[英]How to check whether ifstream is end of file in C++
I need to read all blocks of one large file(about 10GB) sequentially, the file contains many floats with a few strings, like this(each item splited by '\\n'): 6.292611 -1.078219E-266 -2.305673E+065 sod;eiwo 4.899747e-237 1.673940e+089 -4.515213
我需要按顺序读取一个大文件(大约10GB)的所有块,该文件包含许多带有几个字符串的浮点数,如下所示(每个项目由'\\ n'
6.292611 -1.078219E-266 -2.305673E+065 sod;eiwo 4.899747e-237 1.673940e+089 -4.515213
): 6.292611 -1.078219E-266 -2.305673E+065 sod;eiwo 4.899747e-237 1.673940e+089 -4.515213
I read MAX_NUM_PER_FILE
items each time and process them and write to another file, but i don't know when the ifstream
is ended. 我
MAX_NUM_PER_FILE
都读取MAX_NUM_PER_FILE
项并处理它们并写入另一个文件,但我不知道ifstream
何时结束。 Here is my code: 这是我的代码:
ifstream file_input(path_input); //my file is a text file, but i tried both text and binary mode, both failed.
if(file_input)
{
file_input.seekg(0,file_input.end);
unsigned long long length = file_input.tellg(); //get file size
file_input.seekg(0,file_input.beg);
char * buffer = new char [MAX_NUM_PER_FILE+MAX_NUM_PER_LINE];
int i=1,j;
char c,tmp[3];
while(file_input.tellg()<length)
{
file_input.read(buffer,MAX_NUM_PER_FILE);
j=MAX_NUM_PER_FILE;
while(file_input.get(c)&&c!='\n')
buffer[j++]=c; //get a complete item
//process with buffer...
itoa(i++,tmp,10); //int2char
string out_name="out"+string(tmp)+".txt";
ofstream file_output(out_name);
file_output.write(buffer,j);
file_output.close();
}
file_input.close();
delete[] buffer;
}
My code goes wrong, length
is bigger than real file size. 我的代码出错,
length
大于实际文件大小。 I have tried file_input.good()
or !file_input.eof()
, they didn't work, getline(file_input,s)
is good, but it is much slower than read
, i want read
, but i don't know how to check whether ifstream
is end-of-file. 我已经尝试过
file_input.good()
或!file_input.eof()
,它们没有用, getline(file_input,s)
是好的,但它比read
慢得多,我想read
,但我不知道怎么做检查ifstream
是否为文件结尾。
I do my work in WINDOWS 7 with VS2010. 我用VS2010在WINDOWS 7中完成我的工作。
I have searched, but there are not any answer about it, How to open a file using ifstream and keep reading it until the end this link can't answer my question. 我已经搜索过,但没有任何答案, 如何使用ifstream打开文件并继续阅读它直到最后这个链接无法回答我的问题。
Update, Problem solved 更新,问题解决了
Hi everyone, I have figured it out that it's my fault. 大家好,我已经弄清楚这是我的错。 Both
while(file_input.tellg()<length)
and while(file_input.peek()!=EOF)
work fine! while(file_input.tellg()<length)
和while(file_input.peek()!=EOF)
工作正常! while(file_input.peek()!=EOF)
is recommended. while(file_input.peek()!=EOF)
是推荐的。
The extra items written after the end-of-file is the left items in buffer
written in the last time. 在文件结尾之后写入的额外项目是上次写入的
buffer
的左侧项目。
Here is the correct code: 这是正确的代码:
ifstream file_input(path_input);
if(file_input)
{
//file_input.seekg(0,file_input.end);
//unsigned long long length = file_input.tellg(); //get file size
//file_input.seekg(0,file_input.beg);
char * buffer = new char [MAX_NUM_PER_FILE+MAX_NUM_PER_LINE];
int i=1,j;
char c,tmp[3];
while(file_input.peek()!=EOF)
{
memset(buffer,0,sizeof(char)*(MAX_NUM_PER_FILE+MAX_NUM_PER_LINE)); //clear first!
file_input.read(buffer,MAX_NUM_PER_FILE);
j=MAX_NUM_PER_FILE;
while(file_input.get(c)&&c!='\n')
buffer[j++]=c;
itoa(i++,tmp,10);//int2char
string out_name="out"+string(tmp)+".txt";
ofstream file_output(out_name);
file_output.write(buffer,strlen(buffer)); //use the correct buffer size instead of j
file_output.close();
}
file_input.close();
delete[] buffer;
}
while( file_input.peek() != EOF )
{
// code
}
Basically peek()
will read the next char
without extracting it. 基本上
peek()
将读取下一个char
而不提取它。
So you can simply compare it to EOF
. 所以你可以简单地将它与
EOF
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.