Parsing data from a .txt file.

I'm building a Exp calculator for a game called Wurm Online. The game creates log files which I plan to use to track the exp, they are .txt files and look like this....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[11:49:18] Body decreased by 16.4 to 20.000
[11:49:18] Body strength decreased by 14.2 to 20.000
[11:49:23] Mind decreased by 8.16 to 20.000
[11:49:28] Miscellaneous items decreased by 39.4 to 20.000
[11:49:33] Shovel decreased by 42.6 to 20.000
[11:53:54] Mind logic decreased by 13.2 to 20.000
[11:53:54] Body stamina decreased by 9.34 to 20.000
[11:54:58] Digging decreased by 56.7 to 20.000
[11:58:04] Miscellaneous items increased by 39.4 to 59.381
[11:58:04] Repairing increased by 0.000782 to 35.51821
[11:58:05] Mind increased by 8.16 to 28.160
[11:58:05] Mind logic increased by 13.2 to 33.213
[11:58:10] Mind increased by 0.000013 to 28.160345
[11:58:10] Mind logic increased by 0.000019 to 33.213360
[11:58:10] Repairing increased by 0.000782 to 35.51899
[11:58:11] Mind increased by 0.000015 to 28.160360
[11:58:11] Mind logic increased by 0.000015 to 33.213375
[11:58:11] Miscellaneous items increased by 0.000057 to 59.380959
[11:58:11] Repairing increased by 0.000782 to 35.51978
[11:58:12] Mind logic increased by 0.000019 to 33.213394
[11:58:13] Mind logic increased by 0.000019 to 33.213413
[11:58:13] Miscellaneous items increased by 0.000061 to 59.381020
[11:58:13] Repairing increased by 0.000782 to 35.52056
[11:58:15] Mind increased by 0.000015 to 28.160376
[11:58:15] Miscellaneous items increased by 0.000057 to 59.381077
[11:58:16] Repairing increased by 0.000782 to 35.52134
[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


They update in real time as the game goes so I wrote this code to update the file... All it does is open and close the file to check for updates really...

Now for the issue... When I run this using getline function it returns doubles of the same lines alot and I'm completely lost now.. I've been working on solving this issue for 5 hours or so with no luck so if you have any advice please let me know! Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
            {
                string buffer;
                file.seekg(file_end);

                string line;
                while(file.good())
                {
                    getline(file, line);
                    cout << line << endl;
                }
            }
        }else{
            cout << "Unable to open file";
        }
        file.close();
    }
    return 0;
}

Edit.. oops fixed a mistake I did before posting
Last edited on
I still have no solution for this yet... Am I using seekg and tellg correctly? If so then it should just read out each new line...yet its doubling up the output still randomly.


Edit.. just incase anyone is worried about it, this is not a cheat or against the game rules and it will be a public release.
Last edited on
At line 30, lets assume you read the last line of the file into line and print it at line 31.
Next iteration through the loop, file.good() is still true at line 28.
You attempt the getline again, but it fails because there is no more data.
You don't check that the getline was successful, but you go ahead and print whatever was in line from the previous iteration. You have now printed the last line of the file twice.
Ah, I will have to go read up on getline again, Ty.
So I could just do this really...
1
2
3
4
while(getline(file, line))
{
    cout<< line;
}
Last edited on
Yes.
hmm, well I just tested this to see if it's working but I'm getting the same results. Any other ideas?

1
2
3
4
5
string line;
while(getline(file, line))
{
    cout << line << endl;
}

This is what would be in the txt file..
1
2
3
4
5
6
7
[13:57:29] Mind increased by 0.000015 to 28.270084
[13:57:29] Mind logic increased by 0.000015 to 33.348907
[13:57:29] Repairing increased by 0.000710 to 36.57891
[13:57:30] Mind increased by 0.000013 to 28.270098
[13:57:30] Mind logic increased by 0.000015 to 33.348923
[13:57:30] Miscellaneous items increased by 0.000053 to 59.777298
[13:57:31] Mind increased by 0.000013 to 28.270111


This is the output of the program..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[13:57:29] Mind increased by 0.000015 to 28.270084

[13:57:29] Mind increased by 0.000015 to 28.270084
[13:57:29] Mind logic increased by 0.000015 to 33.348907
[13:57:29] Mind logic increased by 0.000015 to 33.348907
[13:57:29] Repairing increased by 0.000710 to 36.57891

[13:57:30] Mind increased by 0.000013 to 28.270098
[13:57:30] Mind logic increased by 0.000015 to 33.348923
[13:57:30] Miscellaneous items increased by 0.000053 to 59.777298

[13:57:30] Mind logic increased by 0.000015 to 33.348923
[13:57:30] Miscellaneous items increased by 0.000053 to 59.777298
[13:57:31] Mind increased by 0.000013 to 28.270111


Edit: If just one line comes down the stream it works fine but sometimes 2+ lines are at the same time then it will print double on them
Last edited on
Wow, this really is a pain in the butt. I've read that getline will only return the eof after it reads so I tried using this..

1
2
3
4
5
6
                do
                {
                    if(!line.empty())
                        cout << line << endl;

                }while(getline(file, line));


but it's got the same results minus the blank lines because the check I put in. I even tried check for file.eof() but same results. Is there a reason that it would not detect eof?

Edit: I check with npp for what the end line characters were and it is \r\n.

When I add a 1 second delay this is how the output is, keep an eye on the time stamp..
Using the old method..
1
2
3
4
5
6
7
                string line;
                while(getline(file, line))
                {
                    if(!line.empty())
                        cout << line << endl;
                    Sleep(1000);
                }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[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
Last edited on
Topic archived. No new replies allowed.