By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Search a word from text file in C++

P: 6
Im a beginner of C++. I want to solve this problem.
I have to search few key words from few text files. How to create a file and do the operation as mentioned .
Sep 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: 13
sin
system("grep text file");

=)

Have you even bothered to start writing that program ?
Sep 21 '06 #2

P: 6
system("grep text file");

=)

Have you even bothered to start writing that program ?

Im now im just trying to create the file and to display wat is in the text file.
But the main thing i need is to search key words in the document.
Sep 21 '06 #3

P: 6
i hav manage to create a file and read it..
the << system("grep text file");>> is it just add in like this?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
// Create a file and write the inputs into the file.
// If the file is already exist, then write the data at the end of the file so that the
// existing data is not overwriten.
// Save the file before exit.
ofstream myFile;
myFile.open("CityFile.txt",ios::app|ios::out);
myFile<<"This file contains the cities name."<<endl<<endl;
myFile<<"Singapore"<<endl;
myFile<<"Bangkok"<<endl;
myFile<<"Phnom Penh"<<endl;
myFile.close();

// Open the file which is saved and display the data in the file.
string line;

ifstream Myfile("cityFile.txt");
if(Myfile.is_open())
{
while(!Myfile.eof())
{
getline(Myfile,line);
cout<<line<<endl;
}
Myfile.close();
}
else
cout<<"Unable to open this file."<<endl;


system("grep text file");

system("Pause");
return 0;
}
Sep 21 '06 #4

P: 13
sin
=)
no, that system() was a joke =)

you can use
line.find(pattern,0);
if it's result != string::npos, your string is found
Sep 21 '06 #5

P: 6
Thanks for the idea...
But i dont really understand ur code.
Wat is the "pattern" stands for? And the number "0" there is equals to wat?
Finaly, u did mention "!= string::npos", how should i use it?
Im very sorry, im just a beginner...so i might need a kind of step by step...
Thanks sin.
Sep 21 '06 #6

P: 6
Forgot to include this part!

For example, in the text file i got "I like to eat Mc Donalds."
Therefore, i want to look the key word "eat" whether is it in the text file, if yes then it will display the word once.
Sep 21 '06 #7

P: 13
sin
The code would be:

Expand|Select|Wrap|Line Numbers
  1. ....
  2. char*    search = "eat"; // search pattern
  3. int    offset; // where it was found (or not (-1))
  4.  
  5. // arg 2 (0) specifies the offset from where to begin seach
  6. if ((offset = line.find(search, 0)) != string::npos) {
  7.     cout << "found '" << search << "' @ offset " << offset << endl;
  8. }
  9. .....
  10.  
Sep 25 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.