c ++ - \_stricmp returnerer forkert værdi, mens strengen svarer. Hvad skal man gøre?

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg har denne kodekode:


                        second = strtok (NULL,"
");
                        logprintf(second);
                        if(\_stricmp(second,"WINDOWS") == 0)


Logprintf udskriver data i en logfil, og den udskriver 'WINDOWS' (uden citater).


men \_stricmp returnerer 13 på en eller anden måde. Så hvis checken aldrig bliver bestået. Jeg har prøvet at lave sscanf/sprintf/andre snorveje, men ingen arbejde. Jeg er ude af ideer.


Den fulde kode:


#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,"
");
                    }
                    if(\_stricmp(first,"ID") == 0)
                    {
                        ID = strtok (NULL,"
");
                    }
                    if(\_stricmp(first,"AUTHOR") == 0)
                    {
                        AUTHOR = strtok (NULL,"
");
                    }
                    if(\_stricmp(first,"VERSION") == 0)
                    {
                        VERSION = atoi(strtok (NULL,"
"));
                    }
                    if(\_stricmp(first,"START\_INSTALL") == 0)
                    {
                        second = strtok (NULL,"
");
                        logprintf(second);
                        if(\_stricmp(second,"WINDOWS") == 0)
                        {
                            cout << "INSTALLAH
";
                            install = true;
                            cout << NAME << "|" << ID << "|" << AUTHOR << "|" << VERSION << "|
";
                        }
                    }
                }
                else
                {
                    cout << "ELSE FIRST: " << first << "
";
                    if(\_stricmp(first,"UNPACK") == 0)
                    {
                        second = strtok (NULL,">");
                        third = strtok (NULL,"
");
                        cout << first << "|" << second << "|" << third << "|
";
                        ToDoVec.push\_back(ToDoInfo(0,second,third));
                    }
                    if(\_stricmp(first,"PRINT") == 0)
                    {
                        second = strtok (NULL,"
");
                        cout << first << "|" << second << "|" << third << "|
";
                        ToDoVec.push\_back(ToDoInfo(3,second,""));
                    }
                    if(\_stricmp(first,"ADD\_PLUGIN") == 0)
                    {
                        second = strtok (NULL,"
");
                        cout << first << "|" << second << "|" << third << "|
";
                        ToDoVec.push\_back(ToDoInfo(1,second,""));
                    }
                    if(\_stricmp(first,"ADD\_FILTERSCRIPT") == 0)
                    {
                        second = strtok (NULL,"
");
                        cout << first << "|" << second << "|" << third << "|
";
                        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 << "|
";
            }
        }
        else
        {

        }
    }
}
found = false;
install = false;
installing = false
CloseZip(hz);

while (FindNextFile(h, &fd))
{
//fnVec.push\_back(fd.cFileName);
}
#else//assuming linux

#endif


Filen 'INSTALL.AIFLIST' ser sådan ud:


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


Alle data læses korrekt i 'buf'.


Kan nogen foreslå, hvordan man løser dette problem?

Bedste reference


Det ser ud som om du ikke regner med linjestykker for CRLF. Åbning af en fil i teksttilstand betyder '\ r \ n' til '\ n', men der er intet i din kode, der gør det. Hvis 'WINDOWS' efterfølges af '\ r \ n', behandler du det som 'WINDOWS \ r' efterfulgt af en '\ n', fordi '\ n' er alt hvad du 'passerer til strtok' . Der er flere mulige løsninger, men man passerer '\ r \ n' til strtok i stedet.

Andre referencer 1


13 er vognreturskarakteren ' ', så din indtastning har en efterfølgende '\ r', som ikke vises ved udskrivning. For at fjerne den skal du passere " " som separatorer til strtok.

Andre referencer 2


13 indikerer at din second streng indeholder "WINDOWS ". er et vognretur-tegn - i Windows afsluttes linjer i tekstfiler med en sekvens, så din getline() funktion ophører ved en ]] og returnerer som en del af strengen.