Thread: search word in string for subword

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Question search word in string for subword

    Hi!
    I'm trying to write a C function that searches a file for given keyword, and returns the first line containing the keyword. It works if I search for whole words, like:

    theFile:
    "Hello, I am a textfile.
    I have two lines."

    function searches theFile for "textfile" and returns: "Hello, I am a textfile".
    But if it search for "xtfile", it can't find the line. What am I doing wrong?

    Code:
     char *readFileAndSearchForKeyWord(char *filename, char *keyWord) {
    	FILE * pFile;
    	long lSize;
    	long runTimes = 0;
    	char * streamLine;
    	pFile = fopen ( filename , "rb" );
    	if (pFile==NULL) {printf("ERROR: '%s' not found in configuration file '%s'\n", keyWord, filename); exit (1);}
    	
    	// obtain the file size.
    	fseek (pFile , 0 , SEEK_END);
    	lSize = ftell (pFile);
    	rewind (pFile);
    	
    	// allocate memory to contain the whole file.
    	streamLine = (char*) malloc (lSize);
    	if (streamLine == NULL) exit (2);
    
    	//Search for keyWord
    	fgets (streamLine, lSize, pFile);
    	while (!strstr(streamLine, keyWord) && (lSize > runTimes)) {
    		fgets (streamLine, lSize, pFile);
    		runTimes++;
    	}
    	
    	// terminate
    	fclose (pFile);
    	free (streamLine);
    	
    	//Error?
    	if (runTimes == lSize)
    	{
    		printf("ERROR: '%s' not found in configuration file '%s'\n", keyWord, filename);
    		exit(1);
    	}
    
    	return streamLine;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You shouldn't cast malloc: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    You might want to use a do-while loop instead of repeating code.

    You free() streamline and then return it. Don't free it or don't return it, otherwise the calling function might try to access free()ed memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why open a text file in binary mode?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Also why do you allocate enough space to store the whole file, when fgets() only reads the file one line at a time?

    > (lSize > runTimes)
    Unless you're searching for a single letter, in a file consists of all the same letter on one single line, the chance of this ever being a false condition is zero.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, pFile ) != NULL ) {
      // do something with the line
    }
    Is usually sufficient for reading text files.
    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
    Aug 2006
    Posts
    2
    I know all that, but will not spend my time on those issues before I know I can use the function. The function is a modified function for reading files, and store them in a buffer.

    Anyone has an answer on how to get strstr() find a part of a word, like search for "ello", and find "Hello, world!"? Can strstr() do that?

    Thanks!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by labatec
    I know all that, but will not spend my time on those issues before I know I can use the function. The function is a modified function for reading files, and store them in a buffer.
    Did you ever consider the possibility that strstr is working just fine, and this other stuff that you are saving until later might actually be the problem?
    Quote Originally Posted by labatec
    Anyone has an answer on how to get strstr() find a part of a word, like search for "ello", and find "Hello, world!"? Can strstr() do that?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       const char text[] = "Hello world";
       const char *found = strstr(text, "ello");
       puts(found ? "found it" : "didn't see nuthin'");
       return 0;
    }
    
    /* my output
    found it
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  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. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM