简体   繁体   中英

How to use strcmp in if statement to compare

I am trying to create a C program that reads data from a .ini file. The data is read into a variable named buffer. The data in the .ini file looks like this:

 [key]
 title= A, H, D, F 

My program looks like this:

 LPCSTR ini = "C:\\conf.ini";
 char var[100];
 GetPrivateProfileString("key", "title", 0, var, 100, ini);
 char* buffer = strtok(var, ",");
 printf("The complete line is %s", var);

 buffer=strtok(NULL, ",");
 printf(buffer);

 while((buffer= strtok(NULL, ","))!=NULL)
     printf(buffer);

The output looks something like this:

The complete line is A, H, D, F
A
H
D
F

Now here what I want to do is compare each letter received in the 'buffer' to character 'A' and if it is true print Yes, else print No. I have tried using strcmp to compare but the .exe file stopped running.

if (strcmp(buffer, "A")==0)
    printf("Hello")

As from your input format, there are spaces present and you're not considering that as a prt of delimiter. So, expectedly, those spaces will be present in the string pointed bu buffer . You can either do

  • Change your delimiter to " ,"
  • Write if (strcmp(buffer, " A")==0) and so on.

That said,

  1. printf(buffer); is a bad style. Either use puts(buffer) ot prefer printf("%s", buffer) .
  2. Check for the NULL return from strtok() before using that returned pointer.

Here are some fixes I made to your code:

  • I put strcmp() checking inside the loop so that all pieces of string extracted from strtok() will be checked for equality with "A" .

    The first string before the delimiter is extracted to buffer , then it continues extracting from buffer until NULL inside the do-while loop.

  • Since your ini file's value has this format of space-after comma ( ", " ), I changed the delimiter from "," to ", " (adding space after comma) .

    Although it won't make much impact in checking for equality with "A" since A won't be affected (because it is on the first part of the value A, H, D, F ), checking the values of letters in-between commas and space (such as H , D , F ) will be affected if we will use "," (comma only) as 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 is %s\n", var);

       // first piece of string (letter) before delimiter
       // changed delimiter from "," to ", "
       char* buffer = strtok(var, ", ");

       // exits if buffer is NULL
       if ( !buffer )
           return;

       do {  
           // prints the letter
           printf("%s", buffer);

           // checking if the extracted piece of code from var is equal with "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 is A, H, D, F
A Yes
H No
D No
F No

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM