std :: getline返回同一行的倍数

时间:2013-03-17 22:17:47

标签: c++ getline

我正在解析一个.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

2 个答案:

答案 0 :(得分:1)

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

这导致打印出一些线条,通过将file_end调整为表示尚未打印线条的值来实现。这些行将在外部while循环的下一次迭代中第二次打印。

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

答案 1 :(得分:0)

解决方案是在更新之前清除()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";
    }
}