Thread: strstr and '\0'

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    12

    strstr and '\0'

    in the following function
    If the line has at least two instances of the search string starting at or after the user-supplied interger offset, dump the part of the line between but NOT including the first two instances to the output file along with a '\n'.

    Code:
    void FileSearch(char *filename, char *OutFile, char *SearchString, int StartSrch)
    {
    
    	char *ptr1,*ptr2,Line[MAXLEN];
    	FILE *fpIn, *fpOut;
    	int Len;
    	fpIn  = Fopen (filename, "r");
    	fpOut = Fopen (OutFile,  "w");
    
    	Len = strlen(SearchString);
    while (fgets(Line,MAXLEN, fpIn) != NULL)
    {
    	ptr1 = strstr(Line + StartSrch, SearchString);
    
    	if(ptr1)
             {
    	   ptr2 = strstr(ptr1 + Len,SearchString);
    	   if(ptr2)
    	    {
    	      
    	     *ptr2     = '\0'';
    	      ptr1    += Len;
                }
             }
    	else ptr1 = Line;
    	   fputs(ptr1, fpOut);
    
    }
    	Fclose(fpIn);
    	Fclose(fpOut);
    }
    If the fputs prints up to the '\0' and I write *ptr2 = '\0'
    it continues printing the next line on the same line.

    also I have a “wrapper.h”
    for the Fopen and Fclose cases.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    shouldn't you put ptr1 += Len + 1; then?

    Also, that is a fancy while loop indentation you got going on there, bud.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    ok that takes care of the space in the begining,
    but it still prints the remainder of what is left after the second SearchString on the next line.
    and thanks
    I like fancy, bud

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not entirely understanding the point of the second '\0', actually. You just skip past it anyway. The lovely Elysia wrote these maybe you should check them out.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    The reason I put the *ptr2 = '\0'';
    was to get fputs to stop printing before the second SearchString
    dump the part of the line between but NOT including the first two instances.

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    If I understood what you wanted correctly, this function I put together should do the trick.

    Code:
    int Con_Dump(char * src_buffer, int offset, char * delimiter, char * ret_buffer)
    {
    	if (!src_buffer || !delimiter) return -1;
    	
    	int i;
    	char * first = strstr(&src_buffer[offset], delimiter);
    	if (!first) {
    		return -1;
    	}
    	
    	first += (strlen(delimiter));
    	
    	char * second = strstr(first, delimiter);
    	if (!second) {
    		return -1;
    	}
    	
    	i = 0;
    	while ( i + first < second) {
    		ret_buffer[i] = first[i];
    		i++;
    	}
    	
            ret_buffer[i] = '\0';
    	return 0;
    }
    Hope that helps.
    Last edited by MeTh0Dz; 10-24-2008 at 01:50 PM. Reason: Forgot one line of code

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    Thanks,
    I will run this around a few times and see what happens

  8. #8
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    tsk tsk tsk this looks like one of the assignments from class, no no no dNNNy think i know who u are
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Matus, are you in his class or are you his professor? Either has some potential hilarious factor.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    ehhhhhhhh wrong
    the correct response would have been
    Code:
    void FileSearch(char *filename, char *OutFile, char *SearchString, int StartSrch)
    {
    
    	char *ptr1,*ptr2,Line[MAXLEN];
    	FILE *fpIn, *fpOut;
    	int Len;
    	fpIn  = Fopen (filename, "r");
    	fpOut = Fopen (OutFile,  "w");
    
    	Len = strlen(SearchString);
    while (fgets(Line,MAXLEN, fpIn) != NULL){
    	ptr1 = strstr(Line + StartSrch, SearchString);
           
    	
    	if(ptr1){
    	   ptr2 = strstr(ptr1 + Len,SearchString);
    	   if(ptr2){
    	      ptr2[-1] = '\n';
    	     *ptr2     = '\0';
    	      ptr1    += Len + 1;
    	      
               } 
             }
    	else ptr1 = Line;
    	   fputs(ptr1,fpOut);
    
    }
    	Fclose(fpIn);
    	Fclose(fpOut);
    }

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If one were using strtok() correctly... the correct response would have been not asking a question at all.

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    there is are are many roads leading to the same location.

    you are quite the helpful bee
    c+ expert

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    you have a lot of criticism by no helpful code!
    take your own advise and go roll around in the grass somewhere.
    C+ expert

  14. #14
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    My code does exactly what you asked. Takes a string and returns the characters between the substring and allows you to pick an offset.

    You can add the newline character if you want at the end.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    12
    YES it did work
    MeThoDz

    and I thank you for your help.
    That master5001 guy seemed like he was more interested in putitng people down than helping
    again thank you

Popular pages Recent additions subscribe to a feed