Sep 15, 2011 at 6:13pm UTC
How can I match a word with one in a text file
Let's say I need to check if a file has the word "Hello" in it.
How could I do that?
I'd appreciate code snippets.
**I'm not in a programming class, don't worry about doing my homework**
Sep 15, 2011 at 6:17pm UTC
Well it's really up to you, you can do it lots of different ways, one such way would be to use a sort of regex, another would be to do a simple comparison like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool wordIsInTextFile(string tosearchfor, string filestring){
fstream file;
file.open(filestring.c_str(),ios::in|ios::binary);
string fromfile = "" ;
while (!file.eof()) {
file >> fromfile;
if (tosearchfor == fromfile){
file.close();
return true ;
}
file.close();
return false ;
}
Last edited on Sep 15, 2011 at 6:18pm UTC