can anyone help me!? i have to create program to search word from text file, but i don't know how can i match word from input to text.. Can anybody help!!!??

First you have to extract a word from your text file.
Second compare it with the word you want to search.
true?-->found!
false?-->extract the next word from the file and compare again etc.
until the end of file is reached.

i can open text file, but i can't compare word ..Could you tell me which function can i use for compare it,,Please... thank you

fstream dict("test.txt",ios::in);

if (!dict.is_open())
{ 
    cout << "Unable to open file";
    system("pause");
    exit(1);
}
       while (! myfile1.eof() )
    {
        getline (myfile1,line);

Edited 3 Years Ago by pyTony: fixed formatting

C++ has a string class which contains a compare method. You certainly have some documentation where you can get info about those. If you can find it you can even do better.

You could use tags. For example:
//Proper libaries//
...

ifstream  infile("text.txt")
ofstream outfile("search.txt")
...
...
...
bool tag=flase;
char Word[500];
//Searches word by word;
do 
{
   cin.getline(Word,500)

//check if word matches;
if(word[500]=="your word"
{
   tag=true;
   break;
}
} while(!infile.eof());

//So basically search the whole file and keep tab on which word you have imported and if your word matches then, break;

can anyone help me!? i have to create program to search word from text file, but i don't know how can i match word from input to text.. Can anybody help!!!??

Not much there to go on but it appears you have no indices therefore can use a simple character compare by stepping through the text. The problem is that this type of compare can be very time consumming.

Alternately, you might be able to use the built in library routines for text string handling and simply look for the target text within the text to be searched. Might be the simplest and fastest way to go.

You should find examples of either method in the complier's docs or any basic programming manual.

i don't know why it doesn't match,,

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{   int i,j=0;

    char word[30];
    char text[300];
    cout<<"Enter word to search \n";
    cin>>word[i];
    fstream dict("test.txt",ios::in);

    if (!dict.is_open())
    { 
        cout << "Unable to open file";
        system("pause");
        exit(1);
    }
    loop:
 while(dict.getline(text,300))

{ 
if(word[j]!=text[j])
{


goto loop;
  j++;
}
else;
cout<<text<<endl;}
 cout<<endl;
system("pause");
return 0;
}

i'm really new,,,so confuse..

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags.
Secondly, avoid goto statements, there are much better ways of achieving the same results. http://www.cprogramming.com/tutorial/goto.html

Secondly there is no word[30] to word[299], they don't exsist so thats a very bad move to make. Also if the character matches or not you still go back and read a new line from your inputfile.

There are probably many more things i could comment on but im not going to for now at least.

Chris

C++ has a string class which contains a compare method. You certainly have some documentation where you can get info about those. If you can find it you can even do better.

I can understand that is is all new to you, and that it is difficult to grasp it all, but why use char array when there is a string class? Why do you refuse to read or look up documentation?
You do #include <string> so why don't you use it?

i got a result:::

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

 int main()
     {
      const int i=20;
      char word[i];
      int Counter;
      cout<<"Enter word to search \n";
      cin.get(word,i);


      char fileName[80];
      string buffer;    // for user input
      cout << "Enter file name: ";
      cin >> fileName;


      ifstream fin(fileName);   
      if(!fin.is_open())
      { 
        cout << "Unable to open file";
        system("pause");
        exit(1);
        }

        Counter=0;
        while(fin!='\0')
        {
         getline(fin,buffer);

         size_t a = buffer.find(word,0);
         if (a != string::npos)
         {
          cout<<buffer<<endl;
          Counter++;
          size_t b = a + 1;
          a = buffer.find (word, b);
          }   
         } 
         if(Counter>1)

 cout << "Total  "<<Counter<<"  tLines\n";
 else
 cout << "Total  "<<Counter<<"  Line\n";
system("pause");
return 0;
}

So,how can i link this program to dos??

Edited 3 Years Ago by pyTony: fixed formatting

Comments
10+ posts, still hasn't figured out code tags - a lost cause

cout<<"Enter word to search \n";
cin>>word;

that should be

cin >> word;

, because word is a char, and word is a char array

while(dict.getline(text,300))

{
if(word[j]!=text[j])
{

Likewise this should be

... 
if(string(text).find(word) != string::npos){
...

, otherwise you are comparing the first character only, and not the whole word

you cant enter the char array as

"cout<<"Enter word to search \n";
cin>>word[i];"

You should get the charter array char by char so Use

for(i=0;i<30;i++)
  cin>>word[i];

Here you must enter 30 chars exactly.

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
Don't bump old threads
This article has been dead for over six months. Start a new discussion instead.