sprintf 字符串操作在 loadrunner 中无法正常工作

sprintf string manupulation not working correctly in loadrunner

我正在构建这样的查询字符串:

#include <stdio.h>

int main()
{
    char filters[300];
    char logicalAnd[5] = " and ";
    char orderNumberFilter[55];
    char punchOrderFilter[50];
    char jobsiteFilter[50];
    char frenteFilter[50];
    char statusFilter[23];

    char orderNumber[] = "pedidos";
    char punchOrder[] = "123456789";
    char jobsite[] = "65119415";
    char frente[] = "65119415";
    char status[] = "B";

    sprintf(orderNumberFilter, "OrderNum eq '%s'", orderNumber);
    sprintf(punchOrderFilter, "PurchOrder eq '%s'", punchOrder);
    sprintf(jobsiteFilter, "Jobsite eq '%s'", jobsite);
    sprintf(frenteFilter, "Frente eq '%s'", frente);
    sprintf(statusFilter, "Status eq '%s'", status);

    sprintf(filters, "%s%s%s%s%s%s", "&$filter=", orderNumberFilter, logicalAnd, punchOrderFilter, logicalAnd, jobsiteFilter);
    printf(filters);

    return 0;
}

我正在使用在线编译器获得此输出:

&$filter=OrderNum eq 'pedidos' and PurchOrder eq '123456789' and Jobsite eq '65119415'

但是当我使用 lr_output_message("filters: %s", filters); 在 loadrunner 中尝试相同的代码来显示结果时,这就是我得到的结果:

&$filter=OrderNum eq 'pedidos' and &$filter=OrderNum eq 'pedidos'PurchOrder eq '123456789' and &$filter=OrderNum eq 'pedidos' and &$filter=OrderNum eq 'pedidos'PurchOrder eq '123456789'Lº¯L•¯L‹Jobsite eq '65119415'

我还尝试了一个我看到的使用 strcpy 的基本示例:

sprintf(orderNumberFilter, "OrderNum eq '%s'", orderNumber);
sprintf(punchOrderFilter, "PurchOrder eq '%s'", punchOrder);
sprintf(jobsiteFilter, "Jobsite eq '%s'", jobsite);
sprintf(frenteFilter, "Frente eq '%s'", frente);
sprintf(statusFilter, "Status eq '%s'", status);

strcpy(filters, "&$filter=");
strcpy(filters, orderNumberFilter);
strcpy(filters, logicalAnd);
strcpy(filters, punchOrderFilter);
strcpy(filters, logicalAnd);
strcpy(filters, jobsiteFilter);

lr_output_message("filters: %s", filters);

这是输出:

Jobsite eq '65119415'

所以... load runnner 是怎么回事?

*将后续的 strcpy 替换为 strcat 作为@Some programmer dude sugested

char filters[300];
char logicalAnd[5] = " and ";
char orderNumberFilter[55];
char punchOrderFilter[50];
char jobsiteFilter[50];
char frenteFilter[50];
char statusFilter[23];

char orderNumber[] = "pedidos";
char punchOrder[] = "123456789";
char jobsite[] = "65119415";
char frente[] = "65119415";
char status[] = "B";

sprintf(orderNumberFilter, "OrderNum eq '%s'", orderNumber);
sprintf(punchOrderFilter, "PurchOrder eq '%s'", punchOrder);
sprintf(jobsiteFilter, "Jobsite eq '%s'", jobsite);
sprintf(frenteFilter, "Frente eq '%s'", frente);
sprintf(statusFilter, "Status eq '%s'", status);

strcpy(filters, "&$filter=");
strcat(filters, orderNumberFilter);
strcat(filters, logicalAnd);
strcat(filters, punchOrderFilter);
strcat(filters, logicalAnd);
strcat(filters, jobsiteFilter);

这是输出:

&$filter=OrderNum eq 'pedidos' and OrderNum eq 'pedidos'PurchOrder eq '123456789' and OrderNum eq 'pedidos'Jobsite eq '65119415'

它更近了但是混乱了。

您在

中有一个未终止的字符串
char logicalAnd[5] = " and ";

如果你写

char logicalAnd[] = " and ";

正如您对其他字符串文字所做的那样,编译器将包含 '[=12=]' 终止符。但是你限制了数组长度,在这种情况下编译器不包括终止符,或者发出警告。

实际上,当您将您认为是 "string" 但不是的东西传递给 strcpystrcat 等字符串处理函数时,行为是未定义的。 =15=]