471,831 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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 62918
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<<"This file contains the cities name."<<endl<<endl;
myFile<<"Phnom Penh"<<endl;

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

ifstream Myfile("cityFile.txt");
cout<<"Unable to open this file."<<endl;

system("grep text file");

return 0;
Sep 21 '06 #4
no, that system() was a joke =)

you can use
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
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))
  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. .....
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 NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.