gpt4 book ai didi

找不到 "Too many arguments for format"的问题

转载 作者:行者123 更新时间:2023-11-30 21:36:24 26 4
gpt4 key购买 nike

我的 sprintf 语句遇到问题。我看到 6 个参数格式和 6 个提供的参数,但出现以下错误:

jsonServer.c:450:4: error: unknown conversion type character ‘}’ in format [-Werror=format=]
sprintf(message, "{\"num_clients\": %d,\"num_requests\": %d,\"errors\": %d,\"uptime\": %u,\"cpu_time\": %lu,\"memory_used\": %l}", (numConnections+1), numRequests, numErrors, uptime, cpuTime, memUsed);
jsonServer.c:450:4: error: too many arguments for format [-Werror=format-extra-args]

char *buildStatus()
{
struct rusage *usage = malloc(sizeof(struct rusage));
int usageRet = getrusage(RUSAGE_SELF, usage);
if (usageRet == -1)
{
perror("RUSAGE fail");
exit(EXIT_FAILURE);
}
long unsigned cpuTime = (usage->ru_utime).tv_sec + (usage->ru_stime).tv_sec;
long memUsed = get_memory_usage_linux();
unsigned int uptime = 0;

char *message = malloc(1000);
sprintf(message, "{\"num_clients\": %d,\"num_requests\": %d,\"errors\": %d,\"uptime\": %u,\"cpu_time\": %lu,\"memory_used\": %l}", (numConnections+1), numRequests, numErrors, uptime, cpuTime, memUsed);
free(usage);
return message;
}

我认为存在一些偷偷摸摸的转义字符问题,但是在到处粘贴反斜杠之后,我似乎无法修复它。

最佳答案

您所要做的就是更正%l(没有这样的说明符),您可能应该使用%ld

char *buildStatus()
{
struct rusage *usage = malloc(sizeof(struct rusage));
int usageRet = getrusage(RUSAGE_SELF, usage);
if (usageRet == -1)
{
perror("RUSAGE fail");
exit(EXIT_FAILURE);
}
long unsigned cpuTime = (usage->ru_utime).tv_sec + (usage->ru_stime).tv_sec;
long memUsed = get_memory_usage_linux();
unsigned int uptime = 0;

char *message = malloc(1000);
sprintf(message, "{\"num_clients\": %d,\"num_requests\": %d,\"errors\": %d,\"uptime\": %u,\"cpu_time\": %lu,\"memory_used\": %ld}", (numConnections+1), numRequests, numErrors, uptime, cpuTime, memUsed);
free(usage);
return message;
}

希望有帮助。

关于找不到 "Too many arguments for format"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53753022/

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