Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help, searching txt file for keyword c++ (http://www.go4expert.com/forums/help-searching-txt-file-keyword-cpp-t21076/)

Marson 23Feb2010 03:29

Need help, searching txt file for keyword c++
 
Hey guys, I am trying to write a program that will use command line arguments to detect user inputed keywords in a text file and output how many times the keyword/s appear. The command line should read like this...

programname -f inputfile keyword1 keyword2...keywordN

The problem I have is I do not know how to search the file for a specific string. For instance, if the keyword to search is "the", I do not know how to make the program read the file and search specifically for the string "the." If anyone can help me out, it would be really appreciated, this is killing me. Here is my code so far:


int main(int argc, char *argv[])
{
int i;

FILE * fptr;
fptr = fopen("myfile.txt","r");

if(fptr == NULL)
{
cout << "file does not exist." << "\n";
}

for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-f") == 0)
{

thillai_selvan 23Feb2010 10:55

Re: Need help, searching txt file for keyword c++
 
In your code you can use the system function.
Consider that you are searching for the "the" keyword from that opened text file
So you can use like follows

system("grep the file_name");

So the system function will execute the grep command and it will search for the key word from that specified file.

Using this you can find the keyword from that file

vivekraj 23Feb2010 10:59

Re: Need help, searching txt file for keyword c++
 
Read the strings given the command line first,then read the string from file using fsanf function.Then,compare the string with keyword using strcmp function.If it matches,then increment the count of the keyword by one.

vivekraj 23Feb2010 11:06

Re: Need help, searching txt file for keyword c++
 
Read the strings given in the command line first,then read the string from file using fscanf function.Then,compare the string with keyword using strcmp function.If it matches,then increment the count of the keyword by one.

Abinila 23Feb2010 11:08

Re: Need help, searching txt file for keyword c++
 
After opening the file you need to read the file by fgets() function. fgets will read line by line from the file. While reading the file itself you can search the keyword from every line of the file.

Sample code,

Abinila 23Feb2010 11:23

Re: Need help, searching txt file for keyword c++
 
After opening the file you need to read the file contents by fgets() function. fgets will read line by line from the file. While reading the file itself you can search the keyword from every line of the file.

Sample code,

lineno=0;
char *p="Pattern"; // Pattern to match.
while((fgets(line,MAX,"file_stream"))!=NULL)
{
lineno++; // Counting the lines.
if((strstr(line,p))!=NULL)
{
/* do some code */
}
}


Note:
strstr() function will match the sub string also.
If you want pure string match , use strcmp() instead of strstr().

xpi0t0s 23Feb2010 14:35

Re: Need help, searching txt file for keyword c++
 
Please use code blocks when posting code.

Marson 23Feb2010 22:46

Re: Need help, searching txt file for keyword c++
 
Thanks for the tips yall, I have actually been working on it quite a bit, and then in the class last night the professor basically went through a lot of it with us because most were having trouble getting this thing working. I don't have much time right now, but I will post what I have gotten done later today/this evening.


All times are GMT +5.5. The time now is 08:25.