我有这样的代码:
second = strtok (NULL,"\n"); logprintf(second); if(_stricmp(second,"WINDOWS") == 0)
logprintf将数据打印到日志文件中,并打印“WINDOWS”(不含引号)。
但_stricmp以某种方式返回13。所以if检查永远不会被传递。 我试过做sscanf / sprintf /其他string的方式,但没有工作。 我没有想法。
完整的代码:
#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'。
有人可以build议如何解决这个问题?
看起来好像你没有考虑CRLF行结尾。 在文本模式下打开文件会将“\ r \ n”翻译成“\ n”,但代码中没有这样做。 如果“WINDOWS”后面跟着“\ r \ n”,那么你就把它当作“WINDOWS \ r”后跟一个“\ n”,因为“\ n”是你传递给strtok
所有东西。 有几种可能的解决方案,但是一个是将“\ r \ n”传递给strtok
。
13是回车符'\r'
,所以你的输入有一个尾部的'\ r',打印时不显示。 要删除它,请将"\r\n"
作为分隔符传递给strtok
。
13
表示你的second
字符串包含"WINDOWS\r"
。 \r
是一个回车符 – 在Windows中,文本文件中的行以\r\n
序列结束,所以您的getline()
函数似乎终止于\n
并返回\r
作为字符串。