gpt4 book ai didi

c++ - std::getline 返回同一行的倍数

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:40 25 4
gpt4 key购买 nike

我正在解析一个 .txt 文件以制作一个 Exp 计算器,但我遇到了一个奇怪的错误,它会将流中超过一行的任何行加倍。我正在使用此代码打开和关闭文件以检查更改并仅打印更改。

这是文本文件的样子..

[11:58:18] Mind increased by 0.000013 to 28.160389
[11:58:18] Mind logic increased by 0.000015 to 33.213428
[11:58:18] Miscellaneous items increased by 0.000061 to 59.381138
[11:58:18] Repairing increased by 0.000782 to 35.52212
[11:58:19] Mind increased by 0.000015 to 28.160404

当我添加 sleep 以减慢速度时,它确实逐行获取每一行并将它们打印到控制台,但在完成后它继续打印流中超过 1 行的每一行..

#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
ios::streampos file_end;
ifstream file ("C://Users//Matthew//wurm//players//Maximillian//logs//_Skills.2013-03.txt");
if(file.is_open())
{
file.seekg( ios::beg, ios::end );
file_end = file.tellg();
file.close();
}else{
cout << "Unable to open file";
}

while(true)
{
ifstream file ("C://Users//Matthew//wurm//players//Maximillian//logs//_Skills.2013-03.txt");
if (file.is_open())
{
file.seekg( ios::beg, ios::end );
streampos new_end = file.tellg();

if(new_end > file_end)
{
file.seekg(file_end);

string line;
while(getline(file, line))
{
if(!line.empty())
cout << line << endl;
Sleep(1000);
}

file_end = new_end;
}
file.close();
}else{
cout << "Unable to open file";
}
}
return 0;
}

这是输出...记下时间戳。我不知道为什么要这样做......

[16:42:36] Mind logic increased by 0.000015 to 33.349262
[16:42:36] Miscellaneous items increased by 0.000053 to 59.777897
[16:42:36] Repairing increased by 0.000694 to 36.59152
[16:42:37] Mind increased by 0.000013 to 28.270370
[16:42:37] Mind logic increased by 0.000019 to 33.349281
[16:42:38] Mind increased by 0.000013 to 28.270384
[16:42:38] Mind logic increased by 0.000015 to 33.349297
[16:42:38] Repairing increased by 0.000694 to 36.59222
[16:42:39] Mind logic increased by 0.000015 to 33.349312
[16:42:40] Repairing increased by 0.000694 to 36.59291
[16:42:42] Mind increased by 0.000015 to 28.270399
[16:42:42] Mind logic increased by 0.000015 to 33.349327
[16:42:42] Repairing increased by 0.000694 to 36.59361
[16:42:43] Mind increased by 0.000013 to 28.270412
[16:42:43] Mind logic increased by 0.000015 to 33.349342
[16:42:44] Mind increased by 0.000013 to 28.270426
[16:42:44] Mind logic increased by 0.000019 to 33.349361
[16:42:44] Repairing increased by 0.000694 to 36.59430
//I added this afterwards to break up what I printed out first... why did it reprint all the times that had 2 or more lines at once?
[16:42:36] Miscellaneous items increased by 0.000053 to 59.777897
[16:42:36] Repairing increased by 0.000694 to 36.59152
[16:42:37] Mind increased by 0.000013 to 28.270370
[16:42:37] Mind logic increased by 0.000019 to 33.349281
[16:42:38] Mind increased by 0.000013 to 28.270384
[16:42:38] Mind logic increased by 0.000015 to 33.349297
[16:42:38] Repairing increased by 0.000694 to 36.59222
[16:42:39] Mind logic increased by 0.000015 to 33.349312
[16:42:40] Repairing increased by 0.000694 to 36.59291
[16:42:42] Mind increased by 0.000015 to 28.270399
[16:42:42] Mind logic increased by 0.000015 to 33.349327
[16:42:42] Repairing increased by 0.000694 to 36.59361
[16:42:43] Mind increased by 0.000013 to 28.270412
[16:42:43] Mind logic increased by 0.000015 to 33.349342
[16:42:44] Mind increased by 0.000013 to 28.270426
[16:42:44] Mind logic increased by 0.000019 to 33.349361
[16:42:44] Repairing increased by 0.000694 to 36.59430

最佳答案

getline() 循环之后,流中的读取位置不保证等于 new_end,因为有人可能在你之后写入文件已确定 new_end,但在您阅读它之前或期间。

这会导致一些行被打印出来,随后将 file_end 调整为一个表明这些行尚未被打印的值。这些行将在您的外层 while 循环的下一次迭代中再次打印。

通过再次调用 tellg() 更新 file_end,而不是通过从 new_end 分配。你必须调用clear()在进行更新之前在流上,因为调用 std::getline() 直到流上的 failbit 被设置,这导致 tellg() 返回 -1(即不是文件末尾的实际位置)。

关于c++ - std::getline 返回同一行的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com