From: dalu.gelu on
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
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
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.