Like this would do:
#include <fstream>
#include <algorithm>
#include <iostream>
int main()
{
std::string input;
std::cout<<"Enter a line to search for: \n";
std::getline(std::cin, input);
std::fstream File("example.txt", std::ios::in);
if (File.is_open())
{
std::string line;
int line_count = 0;
while(std::getline(File, line))
{
if (line.find(input) != std::string::npos)
{
std::cout<<"The line found was: \""<<line<<"\" at line: "<<line_count<<"\n";
if (std::getline(File, line))
{
std::cout<<"The line after that is: \""<<line<<"\"\n";
++line_count;
}
else
{
std::cout<<"There are no lines after that!\n";
}
}
++line_count;
}
File.close();
}
}
With an example file of:
hello world
I am testing
finding lines
you can search for "hello" and it will return line 0 aka the first line..
However, if you turn on find_approximate_line
and searched for "hey world", it will still return line 0 because of the HammingDistance
algorithm.
If you don't care about partial/close matches then you can remove the HammingDistance algorithm and keep using the std::string.find
.
One example output is:
Enter a line to search for:
> hello world
The line found was: "hello world" at line: 0
The line after that is: "I am testing"