468,490 Members | 2,533 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

Search a word from text file in C++

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
7 61797
sin
13
system("grep text file");

=)

Have you even bothered to start writing that program ?
Sep 21 '06 #2
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
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
sin
13
=)
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
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
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
sin
13
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.

Similar topics

4 posts views Thread by suzy | last post: by
1 post views Thread by Andrew | last post: by
1 post views Thread by Prabhat | last post: by
1 post views Thread by ogo796 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.