| | |
c++ find word from text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
Originally Posted by ddanbe
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 do #include <string> so why don't you use it?
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
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
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??
#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??
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
that should be , because word[i] is a char, and word is a char array
Likewise this should be , otherwise you are comparing the first character only, and not the whole word
C++ Syntax (Toggle Plain Text)
cin >> word;
•
•
•
•
while(dict.getline(text,300))
{
if(word[j]!=text[j])
{
C++ Syntax (Toggle Plain Text)
... if(string(text).find(word) != string::npos){ ...
![]() |
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 binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






