0

With the following code, I can find a string of 1 word (in this example I'm looking for "Word"):

ifstream file("file.txt"); string str; while (file >> str){ if (str.find("Word") != string::npos){ //// } } 

But it doesn't work if I want to find, for example, "Computer screen", which is composed of two words.

Thanks

4
  • 1
    Have you tried str.find("Computer screen")? Commented Feb 25, 2014 at 2:17
  • Since file >> str is used, str.find("Computer screen") won't work. Commented Feb 25, 2014 at 2:19
  • 2
    You should probably replace your reading. Something along the lines of is_open() and getline (for example). Note that matching multiple words in a string works just fine in theory. Commented Feb 25, 2014 at 2:19
  • 1
    For continuously growing complexity of parsing situations consider using Regular Expressions, also available from boost::regex for pre c++11 standard. Commented Feb 25, 2014 at 2:20

2 Answers 2

2

file >> str reads a parameter (in this case, a string) delimited with whitespace. If you want to read the whole line (or in any case, more than one word at once), you can use getline operator (reads the string which is delimited by newline by default).

ifstream file("file.txt"); string str; while (std::getline (file,str)){ if (str.find("Computer screen") != string::npos){ //// } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I didn't realize I could use getline that way. So does this mean that getline stores everything from file.txt in str and then I search the str with find?
@mzee99 No. It only reads a single line (meaning up to the next newline character '\n'. Since there is no newline in your txt file it reads until the end of the file.
It reads your input line-by-line (till the next newline symbol). If it succeeds, it provides you a string where the next line is stored (in the example it is written into str), and then you can search it for your parameter. You can redefine the delimiter for getline, by the way: see cplusplus.com/reference/string/string/getline
0

If you know there are two words and what they are, all you need is this:

ifstream file("file.txt"); string str; while (file >> str){ if (str.find("Computer") != string::npos){ file >> str; if (str.find("screen") != string::npos) { //// } } } 

But more likely, you are asking to find a single string that might be two words, or three or more.

Then, can you count on the string being on a single line? In which case, @Ashalynd's solution will work. But if the string might be broken it will fail. You then need to handle that case.

If your file is "small" - i.e. can easily fit in memory, read in the whole thing, remove line breaks and search for the string.

If it is too large, read in lines as pairs.

Something like this:

std::ifstream file("file.txt"); std::string str[2]; int i = 0; std::getline (file,str[i]); ++i; while (std::getline (file,str[i])) { int next_i = (i+1)%2; std::string pair = str[next_i] + " " + str[i]; if (pair.find("Computer screen") != std::string::npos) { //// } i = next_i; } 

All this assumes that the possible white space between the words in the string is a single space or a newline. If there is a line break with more white-space of some kind (e.g. tabs, you need either to replace white-space in the search string with a regex for white-space, or implement a more complex state machine.

Also, consider whether you need to manage case, probably by converting all strings to lower case before the match.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.