JWoodrell 285 Posted March 15, 2014 Share Posted March 15, 2014 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. Quote Link to post Share on other sites
pabigot 355 Posted March 16, 2014 Share Posted March 16, 2014 (edited) #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 March 16, 2014 by pabigot JWoodrell 1 Quote Link to post Share on other sites
JWoodrell 285 Posted March 16, 2014 Author Share Posted March 16, 2014 thanks for the fast reply pabigot. string.h was already included for other stuff, so bonus. if ( strncmp(Line,"[start config]",lineLength-2 ) == 0) works beautifully for me. Quote Link to post Share on other sites
Fred 453 Posted March 16, 2014 Share Posted March 16, 2014 I find you can also learn a lot from looking at how a function like strcmp does its work. Quote Link to post Share on other sites
tripwire 139 Posted March 17, 2014 Share Posted March 17, 2014 [...] 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. mbeals and JWoodrell 2 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.