Reading variables from file

Say I was tracking statistics from a game and I wanted to track damage dealt.

Example:
"2016-06-12 13:53:56 [System] [] You inflicted 52.0 points of damage."

And in order to track dmg I would go through a log row by row until i hit the keyword " You inflicted " and then directly after I would read the variable(52.0).

problem is that the computer wants the variable on the next line after the key is found and I'm not sure how I would go about reading on the same line

Example
"2016-06-12 13:53:56 [System] [] You inflicted
52.0 points of damage."

this would work but doesn't help me.

TLDR: I wish to read a variable from a file directly after a keyword is found and not on the row below


something like:
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
string row;
	string fileName = "TEST.txt";
        double dmg{0},totalDmg{0};
	unsigned int foundKey;
	string key = "You inflicted";
	std::ifstream IN(fileName);

	//failcheck
	if (!IN) {
		std::cout << "file could not open" << std::endl;
		exit(EXIT_FAILURE);
	}

	//check for previous round
	while (getline(IN, row)) {
		foundKey = row.find(key);
		
		if (foundKey != string::npos) {
			IN >> dmg;
			IN.get();
                        totalDmg+=dmg;
		}
	}

have you learned about ini files?
there are many libraries out there you don't need to reinvent the wheel.

http://www.cplusplus.com/forum/unices/80716/
Last edited on
Maybe sth. 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

using namespace std;

int main(void)
{
  string key = "You inflicted";
  string row =  "2016-06-12 13:53:56 [System] [] You inflicted 52.0 points of damage.";
  auto pos = row.find(key);
  double dmg = 0.0;

  if (pos != string::npos)
  {
    row = row.substr(pos + key.size() + 1); 
    // row is now 52.0 points of damage.
    istringstream iss(row);
    iss >> dmg;
    // use dmg
  }
  system("pause");
  return EXIT_SUCCESS;
}
have you learned about ini files?
there are many libraries out there you don't need to reinvent the wheel.

http://www.cplusplus.com/forum/unices/80716/


I have not but sadly the existing file is .log so wouldn't have helped ^^

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

using namespace std;

int main(void)
{
  string key = "You inflicted";
  string row =  "2016-06-12 13:53:56 [System] [] You inflicted 52.0 points of damage.";
  auto pos = row.find(key);
  double dmg = 0.0;

  if (pos != string::npos)
  {
    row = row.substr(pos + key.size() + 1); 
    // row is now 52.0 points of damage.
    istringstream iss(row);
    iss >> dmg;
    // use dmg
  }
  system("pause");
  return EXIT_SUCCESS;
}



That would probably do!
I'm a total noob so few questions for the sake of learning:
does istringstream only converts the first iteration(being 52.0) and in to what?
52.0 would be no problem being an int but if it was 52.2 I need it to be a float

also why system("pause") and exit_success??

I was told I could use regex too but never used them
does istringstream only converts the first iteration(being 52.0) and in to what?

istringstream reads a double until it finds a character that is not a number - in this case the space.
52.0 would be no problem being an int but if it was 52.2 I need it to be a float

It is a double since in the example it was 52.0 it will show as 52. If you change to 52.2 it will show 52.2.
also why system("pause") and exit_success??

system("pause") is only needed if you use Visual Studio to keep the console open.
EXIT_SUCCESS is a way to say that the program ran successfully. You can return 0 instead.
I was told I could use regex too but never used them

Regex are quite advanced stuff and I would not recommend regex to a beginner.
system("pause") is only needed if you use Visual Studio to keep the console open.


oooh I just put std::cin.get() at the end, guess I was too lazy to look up the real solution lol.
but it does keep the console open.

Cheers anyway! I think yours would be the best solution for me right now
Topic archived. No new replies allowed.