小编use*_*362的帖子

C:比较2个字符串

我必须比较2个字符串,一个来自文件,一个来自用户输入,这里是文件:

Password 
abcdefg 
Star_wars 
jedi
Weapon 
Planet 
long 
nail 
car 
fast 
cover 
machine 
My_little
Alone
Love
Ghast
Run Code Online (Sandbox Code Playgroud)

从行中获取字符串的代码很好但是用于比较2个字符串的代码没有给出正确的输出

int main(void){
  int loop, line;
  char str[512];
  char string[512];
  FILE *fd = fopen("Student Passwords.txt", "r");
  if (fd == NULL) {
    printf("Failed to open file\n");
    return -1;
  }
  printf("Enter the string: ");
  scanf("%s",string);
  printf("Enter the line number to read : ");
  scanf("%d", &line);

  for(loop = 0;loop<line;++loop){
    fgets(str, sizeof(str), fd);
  }
  printf("\nLine %d: %s\n", line, str);

  if(strcmp(string,str) == 0 ){
    printf("Match"); …
Run Code Online (Sandbox Code Playgroud)

c structure strcmp

-2
推荐指数
1
解决办法
139
查看次数

标签 统计

c ×1

strcmp ×1

structure ×1