_stricmp返回错误的值,而字符串确实匹配.该怎么办
_stricmp returns wrong value while string DOES match. What to do?
我有这段代码:
second = strtok (NULL,"n");
logprintf(second);
if(_stricmp(second,"WINDOWS") == 0)
logprintf将数据打印到日志文件中,并打印"WINDOWS"(不带引号)。
但是_stricmp以某种方式返回13。。所以if检查永远不会通过。我试过用sscanf/sprintf/其他字符串方式,但都不起作用。我没主意了。
完整代码:
#ifdef WIN32
char buf[65535];
bool found = false;
bool install = false;
bool installing = false;
unsigned int installed = 0;
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(L"./*.AIFPAK", &fd);
char * NAME = NULL;
char * ID = NULL;
char * AUTHOR = NULL;
int VERSION;
HZIP hz = OpenZip(fd.cFileName,0);
ZIPENTRY ze;
GetZipItem(hz,-1,&ze);
int numitems=ze.index;
for (int i=0; i<numitems; ++i)
{
GetZipItem(hz,i,&ze);
if(found == false)
{
if(_wcsicmp(ze.name,L"INSTALL.AIFLIST") == 0)
{
found = true;
UnzipItem(hz,i,buf,65535);
i = 0;
stringstream strx;
strx << buf;
string line;
while (getline(strx, line)) {
char * first = NULL;
char * second = NULL;
char * third = NULL;
first = strtok ((char *)line.c_str()," ");
if(install == false)
{
if(_stricmp(first,"NAME") == 0)
{
NAME = strtok (NULL,"n");
}
if(_stricmp(first,"ID") == 0)
{
ID = strtok (NULL,"n");
}
if(_stricmp(first,"AUTHOR") == 0)
{
AUTHOR = strtok (NULL,"n");
}
if(_stricmp(first,"VERSION") == 0)
{
VERSION = atoi(strtok (NULL,"n"));
}
if(_stricmp(first,"START_INSTALL") == 0)
{
second = strtok (NULL,"n");
logprintf(second);
if(_stricmp(second,"WINDOWS") == 0)
{
cout << "INSTALLAHn";
install = true;
cout << NAME << "|" << ID << "|" << AUTHOR << "|" << VERSION << "|n";
}
}
}
else
{
cout << "ELSE FIRST: " << first << "n";
if(_stricmp(first,"UNPACK") == 0)
{
second = strtok (NULL,">");
third = strtok (NULL,"n");
cout << first << "|" << second << "|" << third << "|n";
ToDoVec.push_back(ToDoInfo(0,second,third));
}
if(_stricmp(first,"PRINT") == 0)
{
second = strtok (NULL,"n");
cout << first << "|" << second << "|" << third << "|n";
ToDoVec.push_back(ToDoInfo(3,second,""));
}
if(_stricmp(first,"ADD_PLUGIN") == 0)
{
second = strtok (NULL,"n");
cout << first << "|" << second << "|" << third << "|n";
ToDoVec.push_back(ToDoInfo(1,second,""));
}
if(_stricmp(first,"ADD_FILTERSCRIPT") == 0)
{
second = strtok (NULL,"n");
cout << first << "|" << second << "|" << third << "|n";
ToDoVec.push_back(ToDoInfo(2,second,""));
}
if(_stricmp(first,"END_INSTALL") == 0)
{
break;
}
}
}
}
}
else
{
if(installing == false)
{
i = 0;
installing = true;
for(unsigned int ix = 0; ix < ToDoVec.size(); ++ix)
{
cout << "|" << ToDoVec.at(ix).action << "|" << ToDoVec.at(ix).string1 << "|" << ToDoVec.at(ix).string2 << "|n";
}
}
else
{
}
}
}
found = false;
install = false;
installing = false
CloseZip(hz);
while (FindNextFile(h, &fd))
{
//fnVec.push_back(fd.cFileName);
}
#else//assuming linux
#endif
"INSTALL.AIFLIST"文件如下所示:
NAME Route Connector Plugin
ID GAMER_GPS
VERSION 1733
AUTHOR Gamer_Z
START_INSTALL WINDOWS
UNPACK RouteConnector/plugins/RouteConnectorPlugin.dll>./plugins/RouteConnectorPlugin.dll
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.amx>./filterscripts/Node_GPS.amx
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.pwn>./filterscripts/Node_GPS.pwn
UNPACK RouteConnector/scriptfiles/GPS.dat>./scriptfiles/GPS.dat
UNPACK RouteConnector/sampGDK/EXTRACTED/libsampgdk-2.2.1-win32/bin/sampgdk2.dll>./sampgdk2.dll
ADD_PLUGIN RouteConnectorPlugin
ADD_FILTERSCRIPT Node_GPS
END_INSTALL
START_INSTALL LINUX
UNPACK RouteConnector/plugins/RouteConnectorPlugin.so>./plugins/RouteConnectorPlugin.so
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.amx>./filterscripts/Node_GPS.amx
UNPACK RouteConnector/examples/other/filterscripts/Node_GPS.pwn>./filterscripts/Node_GPS.pwn
UNPACK RouteConnector/scriptfiles/GPS.dat>./scriptfiles/GPS.dat
PRINT To make this plugin work you must install the sampgdk library from www.github.com/Zeex/ (if you don't have it installed)
ADD_PLUGIN RouteConnectorPlugin.so
ADD_FILTERSCRIPT Node_GPS
END_INSTALL
所有数据都已正确读取到"buf"中。
有人能建议如何解决那个问题吗?
看起来好像没有考虑CRLF行的结尾。以文本模式打开文件会将"\r\n"转换为"\n",但代码中没有这样做的内容。如果"WINDOWS"后面跟着"\r\n",则将其视为"WINDOWS\r"后面跟着一个"\n"。因为"\n"是您传递给strtok
的全部信息。有几种可能的解决方案,但其中一种是将"\r\n"传递给strtok
。
13是回车符'r'
,因此您的输入有一个尾部'\r',打印时不会显示。要删除它,请将"rn"
作为分隔符传递给strtok
。
13
表示您的second
字符串包含"WINDOWSr"
。r
是一个回车字符-在Windows上,文本文件中的行以rn
序列结束,因此您的getline()
函数似乎终止于n
,并将r
作为字符串的一部分返回。