我正在构建一个这样的queryString:
#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'
所以...什么时候继续加载runnner?
*替换strgspy for strcat作为@Some程序员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'
它更接近但却是混乱的。
答案 0 :(得分:3)
中有未终止的字符串
char logicalAnd[5] = " and ";
如果你写
char logicalAnd[] = " and ";
与其他字符串文字一样,编译器将包含'\0'
终结符。但是你限制了数组长度,在这种情况下编译器不包括终结符,或发出警告。
实际上,当你传递你认为是“字符串”的行为时,行为是未定义的,但不是,对于strcpy
和strcat
等字符串处理函数。