N
Newuserms123
Guest
I'm trying to compare 2 strings using CompareStringW API function as below.
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
typedef char * Str;
Str password = "administrator";
int _tmain(int argc, _TCHAR* argv[])
{
char* filename = "C:\\ProgramData\\PMS\\ViewForum\\UserData\\Default_UserData.txt";
char* key ="administrator";
Search_in_File(filename, key);
getch();
}
int Search_in_File(char *fname, char *str)
{
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
int stringLen;
int stringLen1;
WORD locale;
unsigned int length = 0;
int resultComp;
//Visual Studio users
if((fopen_s(&fp, fname, "r")) != NULL)
{
return(-1);
}
while(fgets(temp, 512, fp) != NULL)
{
if(strstr(temp, str) != NULL)
{
find_result++;
if(find_result>0)
{
printf("\n\nFile : %s",temp);
break;
}
}
}
length = strlen(temp);
if (temp[length - 1] == '\n')
{
temp[--length] = '\0';
}
locale = GetUserDefaultLCID();
resultComp = CompareString(locale, SORT_STRINGSORT, temp, -1, password, -1);
printf("\n\nresultComp CompareString : %d",resultComp);
stringLen = strlen(temp);
stringLen1 = strlen(password);
printf("\n\nsize of string passwd, password: %d , %d",stringLen, stringLen1);
line_num++;
}
Here, s2 is hardcoded and temp is read from a txt file.I have removed /n from end of string read from file and both the strings are null terminated. Comparestring is always returning 3(temp>S2) even though strings are equal.tried to compare same strings with strcmp and it seems to be working fine(returns 0).
Note : I'm using visual studio 2008 as the target product is on Windows embedded OS.
API reference : CompareStringW function
Am i doing it wrong? can anyone suggest the possible solution.
Continue reading...
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
typedef char * Str;
Str password = "administrator";
int _tmain(int argc, _TCHAR* argv[])
{
char* filename = "C:\\ProgramData\\PMS\\ViewForum\\UserData\\Default_UserData.txt";
char* key ="administrator";
Search_in_File(filename, key);
getch();
}
int Search_in_File(char *fname, char *str)
{
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
int stringLen;
int stringLen1;
WORD locale;
unsigned int length = 0;
int resultComp;
//Visual Studio users
if((fopen_s(&fp, fname, "r")) != NULL)
{
return(-1);
}
while(fgets(temp, 512, fp) != NULL)
{
if(strstr(temp, str) != NULL)
{
find_result++;
if(find_result>0)
{
printf("\n\nFile : %s",temp);
break;
}
}
}
length = strlen(temp);
if (temp[length - 1] == '\n')
{
temp[--length] = '\0';
}
locale = GetUserDefaultLCID();
resultComp = CompareString(locale, SORT_STRINGSORT, temp, -1, password, -1);
printf("\n\nresultComp CompareString : %d",resultComp);
stringLen = strlen(temp);
stringLen1 = strlen(password);
printf("\n\nsize of string passwd, password: %d , %d",stringLen, stringLen1);
line_num++;
}
Here, s2 is hardcoded and temp is read from a txt file.I have removed /n from end of string read from file and both the strings are null terminated. Comparestring is always returning 3(temp>S2) even though strings are equal.tried to compare same strings with strcmp and it seems to be working fine(returns 0).
Note : I'm using visual studio 2008 as the target product is on Windows embedded OS.
API reference : CompareStringW function
Am i doing it wrong? can anyone suggest the possible solution.
Continue reading...