Searching buffer for substring 
Author Message
 Searching buffer for substring

I have a file formated with a null terminator after each field name.
When I read that file into a char * buffer I get this
pBuff="Field1.Field2.Field3.Field4"  (. = null terminator).  If the
user changes a field name I want to search and replace the file with
the new field name.  Lets say the user changes field3 to field33.
When I do a
 pTemp=strtstr(pBuff, "field33")
pTemp is always "".  How do I get strstr to ignore null terminators
and look at the entire buffer.  Do I have to loop through the entire
file field by field?
Any suggestions would be great.

Thanks



Tue, 23 Nov 2004 22:43:15 GMT  
 Searching buffer for substring


Quote:
> I have a file formated with a null terminator after each field name.
> When I read that file into a char * buffer I get this
> pBuff="Field1.Field2.Field3.Field4"  (. = null terminator).  If the
> user changes a field name I want to search and replace the file with
> the new field name.  Lets say the user changes field3 to field33.
> When I do a
>  pTemp=strtstr(pBuff, "field33")
> pTemp is always "".  How do I get strstr to ignore null terminators
> and look at the entire buffer.

You don't.  'strstr()', *by definition*, terminates parsing
upon encountering a zero character.

Quote:
> Do I have to loop through the entire
> file field by field?

That's one way.

Quote:
> Any suggestions would be great.

How about memcmp()?

-Mike



Tue, 23 Nov 2004 23:44:56 GMT  
 Searching buffer for substring

Quote:
> I have a file formated with a null terminator after each field name.
> When I read that file into a char * buffer I get this
> pBuff="Field1.Field2.Field3.Field4"  (. = null terminator).  If the
> user changes a field name I want to search and replace the file with
> the new field name.  Lets say the user changes field3 to field33.
> When I do a
>  pTemp=strtstr(pBuff, "field33")
> pTemp is always "".  How do I get strstr to ignore null terminators
> and look at the entire buffer.  Do I have to loop through the entire
> file field by field?

You cannot change the behavior of strstr. It's simply as it is.

Quote:
> Any suggestions would be great.

As you are looking for a certain field, you may want to use the
strcmp() function by looping thru the field-file.

Another problem will be to make changes as you discribed in the
file directly and also in one field. You can make such changes
only if the old string and the new one have the same length or
if the new string is shorter, by introducing more free space in
the file or buffer. But replacing field3 with field33 will
not be as easy.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Fri, 26 Nov 2004 13:46:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Searching string for substring question

2. strfile (file substring search) function wanted

3. strfile (file substring search) function wanted

4. substring search

5. search substring case insensitive

6. Substring search function

7. substring search

8. function to search string for substring

9. substring search

10. How to search a LPSTR for a substring?

11. ListBox substring search

12. Case insensitive substring searching

 

 
Powered by phpBB® Forum Software