これが一致しないのはなぜですか?
...
puts (ep->d_name);
if(ep->d_name=="testme"){ printf("ok"); } else { printf("no"); }
...
出力:
testme
no
これが一致しないのはなぜですか?
...
puts (ep->d_name);
if(ep->d_name=="testme"){ printf("ok"); } else { printf("no"); }
...
出力:
testme
no
試す:
if(!strcmp(ep->d_name, "testme"))
または代わりにd_name
作る。string
これは、同じ値を持つ char* を指す 2 つのポインターと比較しているためです。
あなたは本当にするべきです
puts (ep->d_name);
if(strcmp(ep->d_name, "testme")==0){
printf("ok");
}
else {
printf("no");
}
ただし、文字列の使用を検討してください。これにより、必要なセマンティクスが得られます
d_name で渡される値を知る必要があります。
プログラムが「ok」を出力するには、値も「testme」である必要があります。
また、この関数を確認してください: strcmp. 2 つの文字列を比較します。これは基本的に、ここで行っていることです。
例:
/* strcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}