Need help, searching txt file for keyword c++

Marson's Avatar, Join Date: Feb 2010
Newbie Member
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)
{
0
thillai_selvan's Avatar, Join Date: Feb 2010
Go4Expert Member
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
0
vivekraj's Avatar, Join Date: Feb 2010
Light Poster
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.
0
vivekraj's Avatar, Join Date: Feb 2010
Light Poster
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.
0
Abinila's Avatar, Join Date: Feb 2010
Light Poster
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,
0
Abinila's Avatar, Join Date: Feb 2010
Light Poster
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().
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Please use code blocks when posting code.
0
Marson's Avatar, Join Date: Feb 2010
Newbie Member
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.