i trying create c program reads data .ini file. data read variable named buffer. data in .ini file looks this:
[key] title= a, h, d, f my program looks this:
lpcstr ini = "c:\\conf.ini"; char var[100]; getprivateprofilestring("key", "title", 0, var, 100, ini); char* buffer = strtok(var, ","); printf("the complete line %s", var); buffer=strtok(null, ","); printf(buffer); while((buffer= strtok(null, ","))!=null) printf(buffer); the output looks this:
the complete line a, h, d, f h d f now here want compare each letter received in 'buffer' character 'a' , if true print yes, else print no. have tried using strcmp compare .exe file stopped running.
if (strcmp(buffer, "a")==0) printf("hello")
here fixes made code:
i put
strcmp()checking inside loop pieces of string extractedstrtok()checked equality"a".the first string before delimiter extracted
buffer, continues extractingbufferuntilnullinsidedo-whileloop.since
inifile's value has format of space-after comma (", "), changed delimiter","", "(adding space after comma).although won't make impact in checking equality
"a"sinceawon't affected (because on first part of valuea, h, d, f), checking values of letters in-between commas , space (suchh,d,f) affected if use","(comma only) delimiter.
modified code
#include <stdio.h> #include <windows.h> int main() { lpcstr ini = "c:\\conf.ini"; char var[100]; getprivateprofilestring("key", "title", null, var, sizeof var, ini); printf("the complete line %s\n", var); // first piece of string (letter) before delimiter // changed delimiter "," ", " char* buffer = strtok(var, ", "); // exits if buffer null if ( !buffer ) return; { // prints letter printf("%s", buffer); // checking if extracted piece of code var equal "a", // prints either " yes" or " no" ( !strcmp(buffer, "a") ) ? puts(" yes") : puts(" no"); // continues checking until null } while( buffer = strtok(null, ", ") ); } output
the complete line a, h, d, f yes h no d no f no
Comments
Post a Comment