getline返回同一行的倍数
std::getline is returning multiples of the same line
我正在解析一个.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
(即不是文件结束的实际位置)。
解决方案是在更新之前清除()tellg()函数,并且我还以不同的方式更新file_end和new_end。
while(true)
{
ifstream file ("C://Users//Matthew//wurm//players//Maximillian//logs//_Skills.2013-03.txt");
if (file.is_open())
{
if(new_end > file_end)
{
file.seekg(file_end);
string line, output;
while(getline(file, line))
{
//if(!line.empty())
// cout << line << endl;
int f1 = line.find("] ")+2;
int f2 = line.find(" increased ");
if(f1 != line.string::npos && f2 != string::npos)
{
output.append(line, f1, (f2-f1));
output += " ";
f1 = line.find(" by ") + 4;
f2 = line.find(" to ");
if(f1 != string::npos && f2 != string::npos)
{
output.append(line, f1, (f2-f1));
output += " ";
}
}
cout<<output<<endl;
output.clear();
}
file.clear();
file_end = file.tellg();
}
file.seekg( 0, ios::end );
new_end = file.tellg();
file.close();
}else{
cout << "Unable to open file";
}
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何在C++中以这种方式返回一行文本?
- 如何在同一行内打印和返回?
- C 数学问题,如果数学全部在同一行上,则双重返回值
- 为什么此代码只能识别文本文件中的第一行?输入任何大于 1 的数字都会返回"Atomic Number Not Found"
- 即使将返回 0 作为 main 的第一行,程序也无法退出C++
- 在文件中返回一行的函数
- 返回到输入文件 的某一行的开头,C++
- 在文本文件C++上返回一行
- 将指针返回到第一行-QFile
- Sqlite3:返回一行的索引,并通过索引找到一行
- 简单的一行类方法返回错误的值
- 在一行中定义具有相同返回类型和参数类型的多个私有函数
- getline返回同一行的倍数
- 返回所有行,或者只返回最后一行
- c++ -CLI Pipeline只返回错误消息的第一行