| | |
c++ find word from text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
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.
Last edited by ddanbe; Dec 10th, 2008 at 6:59 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
You could use tags. For example:
//Proper libaries//
...
//So basically search the whole file and keep tab on which word you have imported and if your word matches then, break;
//Proper libaries//
...
C++ Syntax (Toggle Plain Text)
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());
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
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!!!??
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.
My advice, do not use eof() it's counter productive. Instead do this.
Chris
C++ Syntax (Toggle Plain Text)
while(getline(file, someString)){ //yourcode }
Knowledge is power -- But experience is everything
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
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..
#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..
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
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
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- search in text file (C++)
- Find a word in a file (Python)
- # of lines in a text file (Java)
- find strings in file.txt (C++)
- string: get token from string and compare token from text file (C++)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: How to Find The user input ?
- Next Thread: Palindrome re-visited
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






