C: Parse empty tokens from a string with strtok

后端 未结 8 1132
臣服心动
臣服心动 2020-12-10 19:08

My application produces strings like the one below. I need to parse values between the separator into individual values.

2342|2sd45|dswer|2342||5523|||3654|         


        
8条回答
  •  甜味超标
    2020-12-10 19:48

    Below is the solution that is working for me now. Thanks to all of you who responded.

    I am using LoadRunner. Hence, some unfamiliar commands, but I believe the flow can be understood easily enough.

    char strAccInfo[1024], *p2;
    int iLoop;
    
    Action() {  //This value would come from the wrsp call in the actual script.
        lr_save_string("323|90||95|95|null|80|50|105|100|45","test_Param");
    
        //Store the parameter into a string - saves memory. 
        strcpy(strAccInfo,lr_eval_string("{test_Param}"));
        //Get the first instance of the separator "|" in the string
        p2 = (char *) strchr(strAccInfo,'|');
    
        //Start a loop - Set the max loop value to more than max expected.
        for (iLoop = 1;iLoop<200;iLoop++) { 
    
            //Save parameter names in sequence.
            lr_param_sprintf("Param_Name","Parameter_%d",iLoop);
    
            //Get the first instance of the separator "|" in the string (within the loop).
            p2 = (char *) strchr(strAccInfo,'|');           
    
            //Save the value for the parameters in sequence. 
            lr_save_var(strAccInfo,p2 - strAccInfo,0,lr_eval_string("{Param_Name}"));   
    
            //Save string after the first instance of p2, as strAccInfo - for looping.
            strcpy(strAccInfo,p2+1);
    
            //Start conditional loop for checking for last value in the string.
            if (strchr(strAccInfo,'|')==NULL) {
                lr_param_sprintf("Param_Name","Parameter_%d",iLoop+1);
                lr_save_string(strAccInfo,lr_eval_string("{Param_Name}"));
                iLoop = 200;    
            }
        }
    }
    

提交回复
热议问题