c - How to use strcmp in if statement to compare -


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 extracted strtok() checked equality "a".

    the first string before delimiter extracted buffer, continues extracting buffer until null inside do-while loop.

  • since ini file's value has format of space-after comma (", "), changed delimiter "," ", " (adding space after comma).

    although won't make impact in checking equality "a" since a won't affected (because on first part of value a, h, d, f), checking values of letters in-between commas , space (such h, 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