Thread: Searching a specific string/word/etc in a text file?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    26

    Searching a specific string/word/etc in a text file?

    I am learning the joys of text file manipulation with C and having a blast However I am having issues trying to find information online on how to search (specifically a string in my case) in a text file. Do I need to use a certain function, or use a for loop to loop through the text file and find it? I am kinda lost. Any help would be greatly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can use a function from the standard library, or you can always "roll your own".

    The standard library function most useful perhaps would be strstr(), which searches for a string, inside a (conceptually) larger string.

    You might start a do while loop and:

    1) use fgets() to take in a line at a time, from the file

    2) use strstr() with the target string having been entered by the user, to search for the target, within each line of text.

    3) loop until the file has reached it's end.

    For strstr(), you'll want to include string.h in the header files. strstr() returns the address of the start of the target string, if it is found, and NULL otherwise.
    Last edited by Adak; 11-28-2009 at 10:49 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Depending on the target string ie if it's a regexp; you can make use of the regcomp() and regexec() functions.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    26
    Hmm. I think this is a bit more complex than what I'm needing. What about searching a text file for one or two words the user inputs (which would be a string in my case - but generally I'm just needing to know the basics of searching and finding something in a text file)?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zacharyrs View Post
    Hmm. I think this is a bit more complex than what I'm needing. What about searching a text file for one or two words the user inputs (which would be a string in my case - but generally I'm just needing to know the basics of searching and finding something in a text file)?
    1) Get the target word from the user, and open the file

    2) While the file has more contents in it

    3) Input a portion of the file into a char array[]

    4) Use strstr() to search the array for the target word(s)

    5) End the loop when the target has been found, or the file has no more content. Your choice.

    6) Close the file

    That's the simplest way to do it. The code is almost simpler to write out than the above description.

    Instead of trying to tell us how easy it should be, why not put your time and effort into something constructive?

    There are *much* more complex ways of doing what you want to do. Very smart programmers have devoted a great deal of time, finding out how to do this, better.

    The above description is as simple as possible in C, and most likely, entirely meets your needs.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Same issue

    I too am having the same problem.

    I addressed it the way Adak mentioned, using strstr. It worked and was easy to implement.

    However, using strstr returns even partial matches, which I can't have. e.g. Searching for "the" returns matches on "there" and "therefore" for instance, which is not ideal.

    I attempted to use sscanf to parse the string returned by fgets, then compare using strcmp, but it stops immediately upon reaching the first whitespace!

    For instance, my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])  {
    
      char line_storage[100], buffer[100];
      int check, line_num = 1;
      FILE *input = fopen(argv[1], "r");
      
      while( fgets(line_storage, sizeof(line_storage), input) != NULL )  {
        check = 0;
        sscanf(line_storage,"%s",buffer);
        if(strcmp(buffer,argv[2]) == 0)  check = 1;
        if(check == 1) printf("Word found on line %d ", line_num);
        line_num++;
      }
    
      return 0;
      
    }
    correctly scans the input text file, but when it reaches sscanf it only parses the first word and then stops.

    Is there any way to use sscanf to parse each word in the array line_storage, moving them to buffer and comparing against the test word?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

Tags for this Thread