how can I serch a word in a txt?

how can I serch a word in a txt?
I have a text and it is for example :"hellow hellow hi hi good bye."
and I want to serch the "bye"word what should I do?
You could use a loop to go through each word in the file until you find the correct one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <limits>

int main() {
std::string input;
std::ifstream infile("myfile.txt"); // You may have a different filename, but this is just an example.
while (input != "bye") {
infile >> input;
}
infile.close(); 
std::cout << input << std::endl; // Just to make sure...
std::cout << "Press the 'Enter' key to close the program.";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}


This will only work if there are newlines between every word in your file, though. You may have to try a different approach if your file really is laid out like that.
Last edited on
oh thanks a lot but the problem is that I can not convert this to VC++ .if you know this could you pleas write this code into VC++?
Sorry it didn't compile. It was because I forgot to include the 'limits' file. It should be fixed now. :)
but it didn't work in VC++
Did you include 'stdafx.h'? Sorry, I do not use Visual Studio. I use Code::Blocks with MinGW.
Last edited on
ok ,
thanks alot for your help.
Your welcome! =D
Topic archived. No new replies allowed.