c++ - Why is my string parsed differently via strtok on Windows and Linux? -


In my program, I am cutting with my four characters * Stroke when I'm checking Windows, I think Like it is cut, but when I'm doing the same thing on Linux, it's doing wrong.

Example:

Windows:

  • My four * (line) is: "1,21-344-32, Blabala". >
  • I get "1"
  • for the first time "21-344-32"

Linux for the first time "strtok"

  • My four * (line) is: "1,21-344-32, Blabala"
  • At first I get "1" do strtok>
  • second I get "2"

code

  Results FileReader :: InitRead (Manager * mngr, char * pfileName, ofstream & Amp; ResultFile) / / Add / * Manager * MNG, * / +: Vehicle init {FILE * pfile; Four file names [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 Waxen = Vehicle :: InvalidCrm; Bool stop = false; String check; Strcpy (filename, pfileName); If ((pfile = fopen (fileName, "r")) == NULL} {ResultFile & lt; & Lt; "Error opening the vehicles, file does not exist, does not exist. \ N"; Refund failed; } And {while (fgets (line, size (line), pfile) = = null) {lcount ++; Wcount = 0; Check.assign (line); If (check.size ()! = 0) {word = strtok (row, ","); If (([[word [0]! = '#') & Amp; amp; amp; amp; words ([0]! = '\ R') & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; \ N ")! = 0)) {wcount ++; Str1 = word; VecEnm = (vehicle :: Vehicle_Type) (atoi (str1)); while ((word! = NULL) & amp; amp; wcount & lt ; 7) & amp; amp; amp;; (stop!)) {Wcount ++; word = string (NULL, ","); switch (wcount) {case 2: str2 = word; 3: str3 = word break; case 4: str4 = word; break; case 5: str5 = word; if ((vchenm; vehicle :: plenim) || (VCENM == vehicle :: invalidcram)) = stop ; Breakage; case 6: str6 = word; break; default: break;}} mng TR-> stratum = str2 = str3 = str4 = str5 = str6 = NULL; stop = False;} // next line reads fclose (pfile);} return success;}  

< P> I can not find anything wrong in your code, but I recommend using strtok_r () instead of strtok () I think strtok should be obsolete, not safe in MT environment Apart from this, I think strtok_r will help you find the bug easily because it has another parameter to track parsing progress, so that it can be easier to find:


Comments