比较字符串从txt文件读取时,CompareStringW无法正常工作。 [英] CompareStringW not working when comparing string read from a txt file.

查看:95
本文介绍了比较字符串从txt文件读取时,CompareStringW无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CompareStringW API函数比较2个字符串,如下所示。 



#include" stdafx .h" b $ b #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 ="管理员" ;;

Search_in_File(文件名,密钥);

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语言环境;

unsigned int length = 0;

int resultComp;



// Visual Studio用户

if((fopen_s(& fp,fname," r"))!= NULL) 

{

return(-1);

}

while(fgets(temp,512,fp)!= NULL) 

{

如果(的strstr(温度, str)!= NULL)

{

find_result ++;

if(find_result> 0)

{

printf(" \\\
\\\
File:%s" ,temp);

break;

}

}

  &NBSP;  }

length = strlen(temp);

  &NBSP; &NBSP; &NBSP; if(temp [length - 1] =='\ n'))
{

  ; &NBSP; &NBSP; &NBSP; &NBSP;   temp [ - length] ='\ 0';

}

区域&NBSP; = GetUserDefaultLCID();

resultComp = CompareString(locale,SORT_STRINGSORT,temp,-1,password,-1);

printf(" \ nn \ nresultComp CompareString:%d",resultComp);

< span style ="white-space:pre"> stringLen = strlen(temp);

stringLen1 = strlen(密码);

printf(" \ n \\ n \\ n \\ n密码:密码:%d,%d&"; stringLen,stringLen1);

line_num ++;

}



这里,s2是硬编码的,从txt文件中读取temp。我从文件的字符串结尾读取了/ n,并且两个字符串都是空终止的。 Comparestring总是返回3(temp> S2),即使字符串相等。比较相同的字符串
和strcmp它似乎工作正常(返回0)。



注意:我正在使用visual studio 2008作为目标产品在Windows嵌入式操作系统上。 

API参考:https://docs.microsoft.com/en-us / windows / desktop / api / stringapiset / nf-stringapiset-comparativetringw



我做错了吗?任何人都可以建议可能的解决方案。




  

解决方案

< blockquote>

当我尝试编译代码时遇到很多错误:函数参数的不兼容类型,没有原型的函数,Search_in_File有一个不返回值的路径,等等。


<您是否剪切并粘贴了代码或重新键入代码?


我找不到任何对CompareStringW的引用。 由于您的所有程序变量都是单字节而不是双字节,因此我不知道您为什么要使用该功能。 



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 : https://docs.microsoft.com/en-us/windows/desktop/api/stringapiset/nf-stringapiset-comparestringw

Am i doing it wrong? can anyone suggest the possible solution.

  

解决方案

I get numerous errors when I try to compile your code: incompatible types for function arguments, functions without prototypes, Search_in_File has a path that does not return a value, etc.

Did you cut and paste your code or retype it?

And I cannot find any reference to CompareStringW.  Since all your program variables are single byte and not double byte, I don't know why you would want to use that function anyway. 



这篇关于比较字符串从txt文件读取时,CompareStringW无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆