|
Prev: Deleting Derived Class Instance Doesn't Call Base's Destructor.
Next: Questions compiled over the last 5 years
From: dalu.gelu on 7 Jun 2008 11:50 Hi, I need to find a pattern in a file, once I find that pattern I need to print 3 lines before to the current line containing the pattern. Please help me how to solve this problem in c++. I am using getline() but not getting any back pointer for prrevious lines. Thanks
From: Daniel T. on 7 Jun 2008 13:43 dalu.gelu(a)gmail.com wrote: > I need to find a pattern in a file, once I find that pattern I need to > print 3 lines before to the current line containing the pattern. > > Please help me how to solve this problem in c++. I am using getline() > but not getting any back pointer for prrevious lines. Keep an array of the last several lines. deque<string> lines( 5 ); ifstream file( "whatever" ); while ( getline( file, lines.back() ) ) { if ( lines.back() == "pattern" ) { // you have your lines. } lines.pop_front(); lines.push_back( string() ); }
From: Gianni Mariani on 8 Jun 2008 02:11
dalu.gelu(a)gmail.com wrote: > Hi, > > I need to find a pattern in a file, once I find that pattern I need to > print 3 lines before to the current line containing the pattern. > > Please help me how to solve this problem in c++. I am using getline() > but not getting any back pointer for prrevious lines. Wouldn't this just be a matter of remembering the last three lines? Nothing specific about C++ here means you're probably OT. |