1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Need help, searching txt file for keyword c++

Discussion in 'C++' started by Marson, Feb 22, 2010.

  1. Marson

    Marson New Member

    Joined:
    Feb 15, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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, "-f") == 0)
    {
     
  2. thillai_selvan

    thillai_selvan New Member

    Joined:
    Feb 20, 2010
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  3. vivekraj

    vivekraj New Member

    Joined:
    Feb 23, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. vivekraj

    vivekraj New Member

    Joined:
    Feb 23, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  5. Abinila

    Abinila New Member

    Joined:
    Feb 20, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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,
     
  6. Abinila

    Abinila New Member

    Joined:
    Feb 20, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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().
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code blocks when posting code.
     
  8. Marson

    Marson New Member

    Joined:
    Feb 15, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page