Jump to content
Sign in to follow this  
JWoodrell

checking a character string in a buffer

Recommended Posts

hey guys working on reading a config file and parsing it.

 

so i read some number of bytes into a buffer (64 byte long)

 

is there an easier way to test is the bytes read match a given string.

 

currently I have to do this

*read bytes*
if (Line[0] == '[' && Line[1] == 's' && Line[2] == 't' && Line[3] == 'a' &&
                            Line[4] == 'r' && Line[5] == 't' && Line[6] == ' ' && Line[7] == 'c' &&
                            Line[8] == 'o' && Line[9] == 'n' && Line[10] == 'f' && Line[11] == 'i' &&
                            Line[12] == 'g' && Line[13] == ']'){
                            printf("it matches");
                        }

this works but is a pain in the butt to work with.

 

but what I would like to do is something more like this

*read bytes*
if (Line == '[start config]'){
printf("it matches");
}

but this fails because "Line" is a 64 byte long buffer and I only read 14 bytes into it, so the rest of the garbage space is still there.

 

does this make sense?  I know how many bytes i read into it is there someway to make it only test x number of bytes from a buffer when testing for equality? or some other thing I can't even thing of.

Share this post


Link to post
Share on other sites

       #include <string.h>

       int strcmp(const char *s1, const char *s2);

       int strncmp(const char *s1, const char *s2, size_t n);
Assuming your toolchain has a proper libc.

 

(edit) In case you don't have man pages: the return value is negative if s1 is lexicographically less than s2, positive if it's greater, zero if they're equal.

Edited by pabigot

Share this post


Link to post
Share on other sites

 

[...] but what I would like to do is something more like this

*read bytes*
if (Line == '[start config]'){
printf("it matches");
}

but this fails because "Line" is a 64 byte long buffer and I only read 14 bytes into it, so the rest of the garbage space is still there.

 

If Line is an array of char, then the reason this won't work is a bit more subtle than that.

 

C and C++ don't support equality comparison of native arrays. That means that (Line == "[start config]") doesn't compare each element of Line with the corresponding elements of the "[start config]" string literal. Instead, Line and "[start config]" both decay to pointers to their first elements. Then the values of those pointers are compared. The two arrays are at different locations in memory, so the two pointers will never match.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...