c++ - 为什么我的字符串在 Windows 和 Linux 上通过 strtok 解析不同?

标签 c++ linux cross-platform token tokenize

在我的程序中,我使用 strtok 切割我的 char*。当我在 Windows 上检查时,它会按我想要的方式进行切割,但是当我在 Linux 上做同样的事情时,它做错了。

示例:

window :

  • 我的 char*(行)是:“1,21-344-32,blabla”
  • 第一次执行 strtok 时得到“1”
  • 第二次我得到“21-344-32”

Linux:

  • 我的 char*(行)是:“1,21-344-32,blabla”
  • 第一次执行 strtok 时得到“1”
  • 第二次我得到“2”

代码

Result FileReader::InitRead (Manager* mngr, char* pfileName, ofstream &ResultFile)//add /*Manager* mng,*/ + use for: vehicle init
{
     FILE *pfile;
     char fileName[50],line[2000], *word= NULL,*str1=NULL,*str2=NULL,*str3=NULL, *str4=NULL, *str5=NULL, *str6=NULL;
     int wcount=0,lcount=0;
     Vehicle::Vehicle_Type vecEnm = Vehicle::InvalidCarEm;
     bool stop = false;
     string check;

     strcpy(fileName,pfileName);

     if((pfile = fopen(fileName, "r")) == NULL) 
     {
         ResultFile<<"Error Opening vehicles init file, May not exist.\n";
         return Fail;
     }
     else
     { 
      while( fgets(line, sizeof(line), pfile) != NULL ) 
      {
       lcount++;
       wcount=0;
       check.assign(line);
       if(check.size()!=0){
       word = strtok (line,",");
       if ((word[0] != '#') &&  (word[0] != '\r') && (strcmp(line,"\n") != 0))
       {
           wcount++;
           str1 = word;
           vecEnm = (Vehicle::Vehicle_Type)(atoi(str1));
           while ((word != NULL) &&  (wcount < 7) && (!stop))
           {
                 wcount ++;
                 word = strtok (NULL, ",");

                 switch (wcount)
                 {
                        case 2: str2 = word;
                              break;
                        case 3: str3 = word;
                              break;
                        case 4: str4 = word;
                              break;
                        case 5: str5 = word;
                              if ((vecEnm < Vehicle::PlaneEm) || (vecEnm == Vehicle::InvalidCarEm))
                                    stop=true;
                              break;
                        case 6: str6 = word;
                              break;
                        default:break;
                 }
            }

            mngr->TranslateVecInit(vecEnm,str2,str3,str4,str5,str6,ResultFile);

           }//while - line not finished
       }
       str1=str2=str3=str4=str5=str6=NULL;
       stop=false;

       }//reads next line
       fclose(pfile); 
       }
 return Success;
}

最佳答案

我无法在您的代码中发现任何错误,但我强烈建议您使用 strtok_r() 而不是 strtok()。我觉得 strtok 应该过时,它在 MT 环境中不安全。 此外,我认为 strtok_r 将帮助您轻松找到错误,因为它有另一个参数来跟踪解析进度,因此很容易找到问题所在: http://www.mkssoftware.com/docs/man3/strtok_r.3.asp

关于c++ - 为什么我的字符串在 Windows 和 Linux 上通过 strtok 解析不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799926/

相关文章:

c++ - Qt 在 Linux 中无法正确读取字符串

c++ - 在类中创建类的实例时,构造函数参数放在哪里?

javascript - 在 PDF 中执行 Javascript

c++ - 如何在 C++ 中获取当前时间和日期?

linux - 为多个发行版和版本构建 Linux 包

c++ - Qt connect() 用法

c++ - 如何正确使用 "C++ Core Guidelines: C.146: Use dynamic_cast where class hierarchy navigation is unavoidable"

linux - Linux 内核代码中的 "current"

linux - 删除 wget yum source sublime2

c++ - 从 C/C++ 库中解压缩可执行文件