Thread: Using C to search through a document

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    Using C to search through a document

    Okay, as part of a program that I am writing I am having it write output to a test file, the problem though, as this is done every 5 minutes the text file is becoming rather large and impracticle to display as is.

    What I am wanting to do is write a piece of code that will have a string entered by the user and then search through the text file for a result.

    As the file has the potential to grow to a verry large size, I am curious as to the best way to accomplish this with minimum fuss.

    If you know of any open source programs that accomplish this task, please feel free to alert me to where I can get a hold of them.

    Thanks.
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Using C to search through a document

    Originally posted by WebmasterMattD

    As the file has the potential to grow to a verry large size, I am curious as to the best way to accomplish this with minimum fuss.
    Use grep
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay, I know that you can use grep to do the searching for you, though is there a way of implementing grep within a program that you have written

    Thanks,
    WebmasterMattD
    WebmasterMattD.NET

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could cheat call system out to grep, but thats a bit nasty and tacky.

    Better if you do something like:
    Open File
    Read A Line
    Check for word in Line: if found, printf it or whatever.
    Read Another line, do check etc etc
    Close File

    Have a go at writing something like that, and post your code here if you get problems.

    I'd suggest functions like fopen(), fgets() (or variant of), strstr() fclose().

    (This is just one way of doing it, someone else may suggest something better).

    Have fun !
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >What I am wanting to do is write a piece of code that will have
    >a string entered by the user and then search through the text
    >file for a result.

    A method:

    Assume the string to search has a length of N characters. Search the file for the first character. If the first character is found, then read the other N-1 characters. Compare the string read from file with the string entered by the user. If the strings are not equal, then search again for the first character.

    This method may not be very efficient, though it is easy to implement.

    For more information use for example Google to search for "string searching". There's a lot of research done on this topic.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Cool, thanks for you help people. I will go about implementing your ideas once I get back from school (boring I know, that it alows me to catch up on the sleep that I miss while programming all night.)

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay, have implemented the first of the ideas.

    Though while it does display a result to the search criterium, it will only display the one that occurs last in the document.

    The script that I am using is pretty much as follows:

    while (fgets(temp, 100, document)) {

    if( strstr(searchfor, temp) ) {
    printf("%s", temp);
    }

    }

    When using fgets is there something else that I should do so that it retrieves all of the instances instead of just the final instance?

    Thanks.
    WebmasterMattD
    WebmasterMattD.NET

  8. #8
    Unregistered
    Guest
    It probably only displays the first one, not the last, although I could be wrong! :+)

    Use a pointer such as:

    Code:
    char *ptr_to_string = NULL;
    
    while (fgets(temp, 100, document)) 
    {
      printf("%s",searchfor); 
      do
      {
        ptr_to_string = strstr(searchfor, temp);
        if(ptr_to_string != NULL) 
        { 
          printf("%s\n", temp);
          ptr_to_string += strlen(searchfor);
        }
       }while(ptr_to_string != NULL);
    }
    Well try something like this anyway. I make no promises.

  9. #9
    Unregistered
    Guest
    Note:
    Change:
    printf("%s",searchfor);
    TO:
    printf("%s",document);

    :+(

  10. #10
    Unregistered
    Guest
    Note2:
    I meant:
    printf("%s", temp);

  11. #11
    Unregistered
    Guest
    Actually this line might still present a problem.

    ptr_to_string = strstr(searchfor, temp);

    Let me think some more.

  12. #12
    Unregistered
    Guest
    Code:
    char *ptr_to_string = NULL;
    // get searchfor here
    
    while (fgets(temp, 100, document)) 
    {
      ptr_to_string = temp;
      printf("%s",temp); 
      do
      {
        ptr_to_string = strstr(searchfor, ptr_to_string);
        if(ptr_to_string != NULL) 
        { 
          printf("%s\n", searchfor);
          ptr_to_string += strlen(searchfor);
        }
       }while(ptr_to_string != NULL);
    }
    This might be more like it. That's if strstr returns NULL if no match was found. I can't test these things without a compiler.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by WebmasterMattD
    Code:
    while (fgets(temp, 100, document)) {
    
      if( strstr(searchfor, temp) )  {
        printf("%s", temp);
      }
    
    }
    This is wrong I believe. Check your documentation on strstr, but mine says you do it this way:
    Code:
    strstr(lookinhere, lookforthis);
    Here's some working code for you to ponder:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char *lines[] = {"this is line one", "and I am 2", "and I am line 3"};
    	char criteria[] = "line";
    	int i;
    	
    	for (i = 0; i < 3; i++)
    	{
    		if (strstr(lines[i], criteria))
    			printf("%s\n", lines[i]);
    	}
    	return (0);
    }
    It simply searches the 3 lines, looking for the word line. If it finds it, it prints it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay now,
    this is the post where I realise how dumb I realy am.

    I forgot that I was using NCURSES which was printing each of the responses ontop of eachother.

    So, I am greateful for your help in originaly getting the base of the code to work.

    Thanks heaps for your help.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >this is the post where I realise how dumb I realy am.
    We all have bad hair days, even us bald folk!

    >I forgot that I was using NCURSES which was printing each of the responses ontop of eachother.
    Good of ya to let us know what went wrong, some people seem to fix it and not let anyone else know. This way we all learn from your mistake

    So, I am greateful for your help in originaly getting the base of the code to work.
    No problem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM