c++ - _stricmp 在字符串匹配时返回错误值。该怎么办?

标签 c++ c windows string compare

我有这段代码:

                        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 << "INSTALLAH\n";
                            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

关于c++ - _stricmp 在字符串匹配时返回错误值。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9385670/

相关文章:

c - 我想在 IAR 工作台编译器中将数据从 long 转换为 ASCII

c - 将带有数组的C代码转换为MIPS汇编

windows - OpenCOBOL 静态链接多个 .cob 文件

c++ - 从代码添加时qt按钮无法点击

c++ - "hides overloaded virtual functions"从多个模板化基类派生时发出警告

c++ - 通用 C++ 架构

c - 无法编译链表程序

java - 我无法在 Windows 10 计算机中安装 Java

windows - 在 Windows 上开发

c++ - Alpha-Beta "breaking"阿姆达尔定律?