我編寫了一個進入文件並將txt文件的每一行復制到數組索引中的程序,然後將該文本行的該行放入另一個數組中按字符分隔線。我試圖將字符數組中的第一個索引與「H」進行比較,但我無法做到這一點。如何將數組內的字符與另一個字符(如「H」)進行比較。如何將字符數組中的值與另一個字符進行比較
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
char const* const fileName = argv[1];
FILE* file = fopen(fileName, "r");
int i = 0;
char line[256];
char* str[256];
while (fgets(line, sizeof(line), file)) {
str[i]=strdup(line);
strcpy(str[i],line);
i++;
}
char tmp[256];
memcpy(tmp, str[0],strlen(str[0])+1);
if(strcmp(tmp[0],"H") == 0){
printf("%s","is h");
}else{
printf("%s","not h");
}
fclose(file);
return 0;
}
我沒試過你的代碼,但'tmp'被正確初始化到正確的值,你可以就可以說'TMP [0] ==「H''。 H'被轉換爲字符「H」的ASCII值,所以只需將該值與'tmp [0]'的ASCII值進行比較就足夠了。 – SpencerD