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)
	   {

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

A couple ways to do it
1) create a structure that contains the keyword and count of the number of times it appears. Then create an array of those structures, one element for each keyword on the command line. Then read each word of the file, for each word loop through the array of keywords and increment the count when a match is found.

2) similar to above but use a <map> instead of an array of structures.

Note: Since you posted this in c++ forum you might want to use ifstream instead of FILE*.

Edited 6 Years Ago by Ancient Dragon: n/a

That sounds like a good solution, but I don't think I can include the keyword in the code as it is inputed by the user on the command line.

I think I should rephrase. The keyword is being input by the user, so I don't think a structure in the code with the keyword and number of times it appears will work since the keyword could be anything. Unless I am misunderstanding, which is completely possible haha

Unless I am misunderstanding, which is completely possible haha

Ancient Dragon didn't say that the keywords are to be hard-coded. So you'll just have to process the command line arguments and gather the keywords for later lookups.

Depending on how many keywords there could be, it might be easier to specify the keywords through a file i.e. these words would also be read in from a file rather than from the command line.

I'm not about to write the program for you, but just show you how to build the list of keywords from the command line arguments.

Lets say you have a structure like this:

struct keywords
{
    std::string word;
    int count;
};

Now all you have to do is build a list of those structures from the command line arguments

#include <vector>
#include <string>
#include <fstream>
using std::vector;
using std::string;
using std::ifstream;

int main(int argc, char* argv[])
{
    vector<keywords> keywordList;
    for(int i = 3; i < argc; i++)
    {
            keywords k;
            k.word = argv[i];
            k.count = 0;
            keywordList.push_back(keywords);
   }
}

I think I should rephrase. The keyword is being input by the user, so I don't think a structure in the code with the keyword and number of times it appears will work since the keyword could be anything. Unless I am misunderstanding, which is completely possible haha

Do you have to count the number of times a keyword is found in the file?
Do you have a list of keywords that you grabbed from the command line?

If so, what's wrong with a structure that you add the keyword to that contains a count you can increment when found in the file?

This article has been dead for over six months. Start a new discussion instead.