gpt4 book ai didi

c - sprintf() 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:40 25 4
gpt4 key购买 nike

我的程序中存在段错误。这是我的代码

char buffer[5000]="";
memset(buffer,0,sizeof(buffer));
sprintf(buffer,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:log=\"http://wsdlclass.wsdlcreat.sims.triesten.com\">\
<soap:Header>\
</soap:Header>\
<soap:Body>\
<log:saveMessBillingDetails>\
<log:userId>%s</log:userId>\
<log:billNo>%s</log:billNo>\
<log:billingAmount>%s</log:billingAmount>\
<log:billingDate>%s</log:billingDate>\
<log:messId>%s</log:messId>\
<log:itemId>%s</log:itemId>\
<log:ipAddress>%s</log:ipAddress>\
<log:schoolId>%s</log:schoolId>\
</log:saveMessBillingDetails>\
</soap:Body>\
</soap:Envelope>",
"00007", "152555", "42.00", "17-08-2013", 10, "CHKK", "10.10.1.164", 1);

最佳答案

当使用 *printf*() 系列函数时,您需要注意转换说明符的 numbertype 与参数匹配遵循“字符串”格式。

在调用 sprintf() 时情况并非如此,因为只有 "%s",其中还有整数(需要 "% d") 被传入。但是参数的数量是正确的。

更新:

您的代码的正确和安全版本可能是:

char buffer[5000]="";
int printed = snprintf(buffer, sizeof(buffer), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:log=\"http://wsdlclass.wsdlcreat.sims.triesten.com\">\
<soap:Header>\
</soap:Header>\
<soap:Body>\
<log:saveMessBillingDetails>\
<log:userId>%s</log:userId>\
<log:billNo>%s</log:billNo>\
<log:billingAmount>%s</log:billingAmount>\
<log:billingDate>%s</log:billingDate>\
<log:messId>%d</log:messId>\
<log:itemId>%s</log:itemId>\
<log:ipAddress>%s</log:ipAddress>\
<log:schoolId>%d</log:schoolId>\
</log:saveMessBillingDetails>\
</soap:Body>\
</soap:Envelope>",
"00007", "152555", "42.00", "17-08-2013", 10, "CHKK", "10.10.1.164", 1);

if (printed >= sizeof(buffer))
fprintf(stderr, "The target buffer was to small.\n");

关于c - sprintf() 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com