Hi, I have a .txt file containing information about 1000 events; each event lists 10-15 hit information. This number is not fixed. The format is given below:
=================================================================
begin of event: 45
Hits(): X = 1.01101 Y = 1.01101 Z = 0.048
Hits(): X = 1.00502 Y = 1.02509 Z = 0.048
...
...
end of event: 45
==================
begin of event: 46
...
...
end of ...
==================
===================================================================
I need to pick up only the last line of each event and write them down in a separate .txt file. Thus, the same task to do for 1000 times. The problem is that here words are mixed up with numbers and it is difficult to pick the choice up. Please see where I got stuck:
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
float xhit,yhit,zhit;
float xend,yend,zend;
string line,firstword;
char start ="begin of event: ";
char stop ="end of event";
ifstream hit_archive("hits.txt");
while (getline(hit_archive, line))
{
ofstream endp;
"?"
endp.open("necessary_hits.txt", fstream::in | fstream::out | fstream::app);
endp<<xend<<yend<<zend<<endl;
endp.close();
}
return 0;
}
This will be the skeleton of the code. But I fear I cannot implement the correct algorithm in the "?".. The algorithm will be following:
when the word "begin of event" is followed by an int, the lines will be written without being appended; that is, the former line will be overwritten. When the word "end of event" is encountered, the loop for next "begin of event" will start.
Can anyone please say if the algorithm is correct and if it is, how to impliment that in the code? Any help will be appreciated.
-CppMan