reading variables off of txt file

Hi, I have a list of data I need to compute in a text file.
The data is formatted like so...

"
Run 141572 event 2
m1: pt,eta,phi,m= 243.862 0.363307 1.14333 0.1 dptinv: 0.000426386

Run 141572 event 3
m1: pt,eta,phi,m= 243.862 0.363307 1.14333 0.1 dptinv: 0.000426386

..."
I need to somehow isolate and assign the variables
run = 141572;
event = 2;
pt = 243.862;
eta = 0.363307;
phi = 1.14333;
m = 0.1;
dptinv = 0.000426386;

I need to somehow collect this data without modifying the list of data. I know a decent amount of c++ but I havent worked much with strings. Can someone help me isolate and assign the variables when the data is formatted like this?
It isn't hard if format is constant.
First line is very simple. I don't think I need to explain it.
The second one is more complicated.

method 1: using stdlib
use file.ignore(4, ' ') to remove 'm1: '
then read 'pt,eta,phi,m=' into a string (with file >>)
then in that string find ','. split the string when you find it.
store the left part somewhere, repeat the process with the left one.
when you can't find ',' remove '=' that is in the end and read 4 numbers (with file >>)
then read a string (>>) and remove ':' from it. read the last number.
then read the '\n'

method 2: doing it yourself
1
2
3
4
5
6
7
8
9
10
11
12
char c;
while(file){
  c = file.get();
  if(c == ' ') break;
}
string str;
while(file){
  c = file.get();
  str.append(c);
  if(c == ',' || c == '=') break;
}//repeat 4 times
//then do as in previous 


method 3:
both of those aren't great though. stdlib is not really designed for parsing. Your own functions have potential to be more flexible, but doing everything yourself isn't comfortable. You should probably look into some special library. maybe boost::regex. (edit: note: I really don' know anything about it)
Last edited on
Topic archived. No new replies allowed.