BTW, what does null terminated strings mean? I presume the getline function does give a null terminated string (I could be very wrong)?
By null terminated string I mean the C-style strings that use '\0' to denote one past the last character of the string. As for getline, that depends on which getline you refer to. In this case, std::getline works with std::string.
I have no clue as to what data you are working with, so it could well be a problem with your data rather than with your code. To convince yourself that find works, try:
Code:
#include <string>
#include <iostream>
int main() {
if (std::string("hello world!").find("world") != std::string::npos) {
std::cout << "found" << std::endl;
} else {
std::cout << "not found" << std::endl;
}
}
By the way, you probably shouldnt be testing input.eof() in your loop condition. Rather, test with getline directly.
Code:
string currentLine;
int line = 0;
ifstream input(file);
while (getline(input, currentLine))
{
++line;
std::size_type pos = currentLine.find(contains);
if (pos != string::npos)
{
cout << "[" << line << "] " << pos << "\n\n";
}
}