为什么这段代码总是打印 "not matched"?

Why does this code always print "not matched"?

#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE *ls = popen("tmp.sh", "r");
    char char_array[256];
    while (fgets(char_array, sizeof(char_array), ls) != 0) {
       //NOP
    }
    char *ptr_somechar = &char_array[0];
    char *pointer = "high";
    if (strcmp(pointer, ptr_somechar) == 0) 
    { 
        printf("%s\n", "match");
    } else 
    { 
        printf("%s\n", "not matched");
    }
    pclose(ls);
    return 0;
}

我想将输出与线进行比较。 tmp.shreturns一个"high"。为什么这段代码总是打印 "not matched"?

似乎文件中的字符串 "high" 后跟一个换行符,fgets 也读作 \n。您需要在比较之前删除该字符。