1

I've got a problem. So i'm using strtok on strings with the same syntax, when i strtok the first string, it's ok, no problem, but when other strings comes in strtok doesn't work. It can't split up my string at the commas. Here's the code:

               i=0;
               po = 0;
               po = strtok(inputStreamBuffer,",");


               while (po != 0){
                     UART1_Write_Text(po);
                     UART1_Write_Text("             ");

                     if (i == 2){

                        if (strlen(po) < 15)

                             strcpy(bufLongitude,po);

                     }

                     if (i == 4){
                         if (strlen(po) < 15)

                             strcpy(bufLatitude,po);

                     }


                     if (i == 6){

                         if (strlen(po) < 3)
                              strcpy(TTFF,po);
                     }

                     if (i == 7){

                         if (strlen(po) < 3)
                             strcpy(numberOfSatellites,po);
                     }

                     if (i == 8){

                         if (strlen(po) < 10)
                              strcpy(speed,po);
                     }

                     if (i == 9){

                         if (strlen(po) < 12)
                               strcpy(courseOverGround,po);
                               courseOverGround[12] = '\0';
                     }

                     i++;

                     po = strtok(0,",");
               }
               po = 0;

Cheking the length of string is a fail-safe thing for me. If something goes wrong, i don't want my program to copy a much larger string into my variable, it would destroy everything, speaking of experience. Also my method works fine for the first string.

First try: Input string example: 1,2,3,4,5,6,7,8,9,10 outputs: var1: 1 var2: 2 var3: 3 and so an...

Other trys, input string is the same, outputs: var1: 1 var2: 2,3,4,5,6,7,8,9

maybe other parts of the code troubles this method...

Hope someone got some idea.

Regards,

tamasJozsa
  • 86
  • 1
  • 12

0 Answers0