I have a binary file (not a text file), about 20M in size, and I have a string which may or may not exist in that file. Normally (for a text file), I would use getline()
to read the file line by line and then use find
to detect it, something like:
bool found = false;
{
std::string stringToLookFor("string to look for");
std::ifstream ifs("myBinaryFile.bin");
std::string line;
while (!found && getline(ifs, line)) {
found = (line.find(stringToLookFor, 0) != std::string::npos);
}
ifs.close();
}
However, I'm unsure if that's a wise thing to do for a binary file. My main concern is that the "lines" for such a file may be large. It may be that the entire 20M file contains no newlines so I may end up reading in a rather large string to search (there may well be other problems with this approach as well, hence my question).
Is this considered a viable approach or am I likely to run into problems? Is there a better way to search binary files than the normal textual line-by-line?