| | |
Need help!!!search word from text file and display lines which match the word.
![]() |
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
Re: Need help!!!search word from text file and display lines which match the word.
0
#1 Dec 4th, 2008
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
Hi,I am Really new in C++. I'm trying to write a program which search word from text file and display matched word whole lines. Can any body help me out please?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{ int i;
char word[30];
char text[50];
fstream dict("bufferfile.txt",ios::in);
if (!dict.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}
cout<<"Enter word to search \n";
cin>>word;
loop:
i=0;
while(dict.getline(text,50))
{
while((word[i]!='\0')&&(text[i]!='\n'))
{ if(word[i] != text[i])
{
goto loop;}
else
cout<<text<<endl;
}
} ++i;
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{ int i;
char word[30];
char text[50];
fstream dict("bufferfile.txt",ios::in);
if (!dict.is_open())
{
cout << "Unable to open file";
system("pause");
exit(1);
}
cout<<"Enter word to search \n";
cin>>word;
loop:
i=0;
while(dict.getline(text,50))
{
while((word[i]!='\0')&&(text[i]!='\n'))
{ if(word[i] != text[i])
{
goto loop;}
else
cout<<text<<endl;
}
} ++i;
system("pause");
return 0;
}
Re: Need help!!!search word from text file and display lines which match the word.
0
#3 Dec 4th, 2008
A list of things to do:
Use code tags, realize you're programming in C++ not BASIC avoid
Use code tags, realize you're programming in C++ not BASIC avoid
goto , avoid system() whenever possible(it's slow, often unportable, and insecure in some cases), and reformat your code because someplaces look like errors but I can't tell. "Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 0
Re: Need help!!!search word from text file and display lines which match the word.
0
#4 Dec 4th, 2008
![]() |
Similar Threads
- Limiting grep searches to a given file type? (IT Professionals' Lounge)
- trouble with counting letter from file, please help. (C++)
Other Threads in the C++ Forum
- Previous Thread: Need Help!!!
- Next Thread: find string in txt file
| Thread Tools | Search this Thread |
anyfile api array based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer development directshow dll download draw dynamic dynamiccharacterarray email encryption error file forms fstream function functions game gdi+ givemetehcodez gnu graph gui homeworkhelp homeworkhelper http iamthwee ibm ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output packing parameter pointer problem program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video visualizationtoolkit win32 windows winsock word wordfrequency wxwidgets





