Check if a file contains a string
May 5, 2014 at 5:13pm
Hi,there is a simple way to check if a file contain a string?
Thanks in advance
May 5, 2014 at 5:56pm
Open file, read one line, Search that line for a match. Repeat until end of file.
I opened the file from argv[1] and used search string from argv[2].
To be a little more useful I would add a line count, and when you find a match display the line number as well as the matching line.
1 2 3 4 5 6 7
|
while ( std::getline( file, line ) )
{
if ( line.find( argv[2] ) != std::string::npos )
{
std::cout << line << std::endl;
}
}
|
Topic archived. No new replies allowed.