#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;
}
str and first_field are my two strings. By puts(), when both the strings are equal they print same output but strcmp() returns non zero value? Aren't str and first_field being considered as strings? When I tried to print their lengths by strlen, it didn't show any output.
strlen, it will likely be different due to non-printable charactersstrcmp()was intended to be used with ASCII. If applied to UTF-8, it will work for identical encoded strings. However, in Unicode/UTF-8 are in certain cases multiple way to express the same glyph e.g. 'ü' can be code point 252 or a 'u' and a diaresis (the upper double points). In this case, it looks equal butstrcmp()will consider this as different.char*, which is pointless since arrays decay to pointers without any cast. It's just not idiomatic to stick a cast there.