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

You are trying to do too much work, especially for a large dataset.

If you were to look at the list of hits on a piece of paper, how would you find the last one for each event? Yes, you would find the words "end of event" and look at the last non-blank line above that.

The algorithm should do exactly the same thing -- just look for the words "end of event" and then print the information on the last non-blank line. This will, of course, require you to remember the last non-blank line read before the line starting with the words "end of event".

Keep in mind that there is no point in parsing information about the hit until you have the line containing that information.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

inline string& trim( string& s )
  {
  s.erase(    s.find_last_not_of ( " \f\n\r\t\v" ) + 1 );
  s.erase( 0, s.find_first_not_of( " \f\n\r\t\v" )     );
  return s;
  }

int main()
  {
  string last_line, curr_line;

  while (getline( cin, curr_line ))
    {
    // Ignore empty lines
    if (trim( curr_line ).empty()) continue;

    // If we have found the end of an event...
    if (curr_line.find( "end of event" ) == 0)
      {
      // Scan the X,Y,Z hit information from the last non-blank line
      istringstream ss( last_line );
      string hits, varname, equalsign;
      double x = 0.0, y = 0.0, z = 0.0;
      ss >> hits
         >> varname >> equalsign >> x
         >> varname >> equalsign >> y
         >> varname >> equalsign >> z;

      // And keep the hit information we want
      cout << x << " " << y << " " << z << endl;
      }

    last_line = curr_line;
    }

  return 0;
  }

Once you have compiled the program, you can use it to filter your hit information normally: C:\Foo> a.exe < hits.txt > necessary_hits.txt Good luck and enjoy the freebie.

Thank you very much my friend. I am suffering from a fever now...Hope I will be able to reply to your post soon.

Hi...I used the code which is being compiled without any error. However, there is a runtime error: it is doing nothing at all. I am going through this code to understand it.
Thank you

-cppman

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.