954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ find word from text file

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!!!??

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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);

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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;

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

thank you so much for advice,,, i'll try

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 
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.

swyncoop
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

My advice, do not use eof() it's counter productive. Instead do this.

while(getline(file, someString)){
//yourcode
}

Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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

#include
#include
#include
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<

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 

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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

thank you

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 
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 so why don't you use it?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

i doesn't return to loop ,, how can i do??

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 

i got a result:::

#include
#include
#include
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<1)

cout << "Total "<

ojung
Newbie Poster
10 posts since Nov 2008
Reputation Points: 5
Solved Threads: 0
 
cout<<"Enter word to search \n"; cin>>word[i];


that should be

cin >> word;


, because word[i] 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

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

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.

onksssss
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You