gpt4 book ai didi

c - C中的字符串比较函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:23 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>

int in_mot(char *str) //check whether input from text file in mot table
{
FILE *fp_mot;
int i = 0, j = 0, r;
char check[300], first_field[7];

fp_mot = fopen("mot.txt", "r");

while (1)
{
if (fgets(check, sizeof(check), fp_mot) == NULL)
{
printf("EOF");
return 0;
}
else
{
i = 0;
for (i = 0; i < 7; i++)
first_field[i] = 0;

i = 0;
while (check[i] != ' ')
{
first_field[i] = check[i];
i++;
}
first_field[i] = '\0';

if (strcmp((char*)str, (char*)first_field) == 0)
{
puts(first_field);
return 1;
}
}
}
if (j == 18)
return 0;
}

int main()
{
char temp[30], wc[10], str[4][10];
int i = 0, j = 0, k = 0, z;
FILE *fp;

int LC = 0, RC = 0, STP = 1;

fp = fopen("ip.txt", "r");

while (1)
{ //1-break a line and store in array of strings(str)
if (fgets(temp, 30, fp) == NULL)
break;
else
{
j = 0; i = 0;
while (j < 3)
{
k = 0;
for (z = 0; z < 10; z++)
wc[z] = 0;
while (temp[i] != ' ' && temp[i] != ',')
{
wc[k] = temp[i];
i++;
k++;
}
i++;
wc[k] = '\0';
strcpy(str[j], wc);
j++;
}
}

//str[0][3] = '\0';
if (in_mot(str[0]))
{
//printf("\n yesssssssss");
}
}

return 0;
}

strfirst_field 是我的两个字符串。通过 puts(),当两个字符串相等时它们打印相同的输出但 strcmp() 返回非零值? strfirst_field 不是字符串吗?当我尝试通过 strlen 打印它们的长度时,它没有显示任何输出。

最佳答案

尝试打印每个字符代码,而不是只显示(可打印)的字符。然后你会发现两个字符串之间有什么不同。

喜欢,

char* p = str;
while (*p)
{
printf("%02x ", p++);
}

关于c - C中的字符串比较函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com