1

Exp Calculatorを作成するために.txtファイルを解析していますが、ストリームから1行を超える行が2倍になるという奇妙なエラーが発生します。このコードを使用してファイルを開いたり閉じたりして、変更を確認し、変更のみを印刷しています。

これは、テキストファイルがどのように見えるかです。

[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

スリープを追加して速度を落とすと、各行が1つずつ取得されてコンソールに出力されますが、完了後、ストリームに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
4

2 に答える 2

1

-loop の後、getline()ストリーム内の読み取り位置が と等しいとは限りません。これは、 を決定した後で、ファイルから読み取りを行う前または読み取り中にnew_end誰かがファイルに書き込みを行った可能性があるためです。new_end

file_endこれにより、一部の行が印刷され、その後、行がまだ印刷されていないことを示す値に調整されます。これらの行は、外側のwhileループの次の反復中に 2 度目に出力されます。

から代入するのではなく、再度file_end呼び出して更新します。更新を行う前にストリームで呼び出す必要があります。これは、ストリームで が設定されるまで呼び出されるためです。これにより、戻りが発生します (つまり、ファイルの末尾の実際の位置ではありません)。tellg()new_endclear()std::getline()failbittellg()-1

于 2013-03-17T22:55:37.690 に答える
0

解決策は、更新前に tellg() 関数を clear() することでした。また、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";
    }
}
于 2013-03-18T14:47:48.980 に答える