使用ifstream进行文件解析 [英] File parsing using ifstream
问题描述
大家好
如果使用以下方法查找文件的大小,然后有效地读取文件的每个字符,直到文件结尾:
Hi All
If you use the following to find the size of a file and then effectively read every character of the file until the end of the file:
long begin, end, size;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.seekg (0, ios::beg);
size = end - begin;
do
{
myfile.get();
size--;
}while( !myfile.eof() );
运行此命令后,我期望大小等于0,但是大约为114000.这是否减小到文本文件中的空白或其他内容???我知道它快到文件末尾了,因为我暂时在其中添加了一些额外的代码以捕获最后20个字符,并且它与文件末尾相匹配.
任何帮助表示赞赏.
谢谢
安迪
更新-现在已将这个确切的代码作为一个单独的项目作为控制台应用程序运行,并且遇到了相同的问题,因此不是导致问题的其他代码.
After running this I was expecting size to equal 0 however it was approx 114000. Is this down to whitespace in the text file or something else??? I know it is getting to the end of the file as I temporarily put some extra code in to catch the last 20 characters and it matched the end of the file.
Any help appreciated.
Thanks
Andy
Update - Have now run this exact code as a seperate project as a console app and am having the same problem so it is not other code causing the problem.
推荐答案
原因是:当您使用myfile.get()
从文本文件中获取字符时,ifstream
对象以翻译模式从文件中读取了新字符,即它替换了 CR-LF 从文件到单个 CR 字符的顺序.
另一个问题是ifstream::eof()
方法不会在没有更多字符要读取时返回true,而只有在您读取文件末尾的新字符之后才返回true.
您可以通过以下方式解决问题:
The reason is this: when you get characters from the text file usingmyfile.get()
, theifstream
object read a new character from the file in translated mode, i.e. it replace CR-LF sequences from the file to a single CR character.
Another issue is that theifstream::eof()
method returns true not when there are no more characters to read, but only after that you have read a new character past the end of the file.
You can fix your problem this way:
long begin, end, size;
ifstream myfile("example.txt");
begin = myfile.tellg();
myfile.seekg(0, ios::end);
end = myfile.tellg();
myfile.seekg(0, ios::beg);
size = end - begin;
ifstream::int_type character;
while ((character = myfile.get()) != EOF)
{
if (character == '\n') size -= 2;
else size -= 1;
}
我刚刚尝试了一下,直到size
最后等于1,这似乎是正确的.也许您还有其他一些代码破坏了您的size
变量.我也注意到了这行
I just tried this and got to the end withsize
being equal to 1 at the end, which seems correct. Perhaps you have some other code that is corrupting yoursize
variable. I did also notice the line
size = end - beg;
在我的系统上无法编译,我认为第二个变量应该是begin
.
我以前的测试有缺陷.如果我使用非文本文件,那么size
会递减到-1,因为即使在文件条件结束时,您总是在myfile.get()
调用后减小其值.[/edit]
which does not compile on my system, I think the second variable should be begin
.
[edit]My previous test was flawed. If I use a non text file then size
counts down to -1 because you always decrement its value after the myfile.get()
call, even if that was the end of file condition.[/edit]
这篇关于使用ifstream进行文件解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!