Thread: Help with .txt search.

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    4

    Help with .txt search.

    Hey everyone!

    I'm working on a program that reads in a .txt file and searches through the text for a keyword. If it gets a hit on the keyword, the line number where the keyword is located and the line that contains the keyword is printed out. What I have now doesn't catch every occurance of the keyword "a". Any ideas why?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        char line[1024];
        char copy[1024];
        char * pch;
        int line_count = 0;
        char test[2] = "a"; 
        FILE* file;
        file = fopen(argv[1], "r");
        
        if(file != NULL)
        {
            while( fgets(line, sizeof line,file) != NULL)
            {
                strcpy(copy,line);
                line_count++;
                pch = strtok (line, " ");
                while(pch != NULL)
                {
                    if( strcmp(pch, test) == 0)
                    {
                        
                        printf("\n%d:%s\n", line_count, copy);
                    }
                    pch=strtok(NULL, " ");
                }
            }
        }
        else{printf("\n\n\tError\n");}
        
        return 0;
        
        
    }//end main
    Here is the text file I have been using to test the code. (It's an quote for the Godfather!)
    Code:
    I raised my daughter in the American
    fashion; I gave her freedom, but
    taught her never to dishonor her
    family.  She found a boy friend,
    not an Italian.  She went to the
    movies with him, stayed out late.
    Two months ago he took her for a
    drive, with another boy friend.
    They made her drink whiskey and
    then they tried to take advantage
    of her.  She resisted; she kept her
    honor.  So they beat her like an
    animal.  When I went to the hospital
    her nose was broken, her jaw was
    shattered and held together by
    wire, and she could not even weep
    because of the pain.
    Thanks!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    More experienced people correct me if I'm wrong, but it sounds like you want to find any occurrence of the letter 'a' regardless of if it's alone or in a word, but what you're doing is breaking each line of the file up into, more or less, words. And those entire words are being string-compared against the string "a". This means that you will only find lines that have the letter "a" all by itself on it.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    4
    Actually that's what I'm trying to do! Find only the occurances of "a" all by itself. But when run with the text file I have above it only finds the "a" on line 4 "famlily, she found a boy friend" and doesn't find the "a" on line 7 "Two months ago he took her for a". I can't think of any reason why it would not find all occurances of "a" all by itself.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your final token at the end of the line will be "a\n".

    To fix this, make your strtok calls like this
    strtok (line, " \n");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    4
    That was it! Thank you all for your help. Happy coding!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Advanced Search -> Search Multiple Content Types
    By phantomotap in forum Tech Board
    Replies: 2
    Last Post: 05-21-2011, 07:28 AM
  3. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  4. Allowing my search function to search sub directories!
    By Queatrix in forum Windows Programming
    Replies: 10
    Last Post: 09-30-2005, 04:54 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM