Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C sprintf没有打印最后一个条目_C - Fatal编程技术网

C sprintf没有打印最后一个条目

C sprintf没有打印最后一个条目,c,C,sprintf没有为变量stats->info.transferID提供正确的值,但是 printf为该变量提供了正确的值,所有其他值都是正确的 char buff[200]; sprintf(buff,"Index:1:%u:%u:%d\n", stats->connection.peer, stats->connection.local, stats->info.transferID); printf

sprintf没有为变量stats->info.transferID提供正确的值,但是 printf为该变量提供了正确的值,所有其他值都是正确的

char buff[200]; 
sprintf(buff,"Index:1:%u:%u:%d\n",
            stats->connection.peer,
            stats->connection.local,
            stats->info.transferID);
printf("  %s",buff);
printf("  %d\n",stats->info.transferID);
信息是传输信息类型的结构

typedef struct Transfer_Info {
    void *reserved_delay;
    int transferID;
    ----
    ----
 }
我得到的输出:

Index:1:2005729282:3623921856:0

3
缓冲区的大小足以容纳其值

提前感谢

对我有用:

#include <stdio.h>

struct connection
{
  unsigned peer, local;
};

struct info
{
  int transferID;
};

struct stats
{
  struct connection connection;
  struct info info;
};

int main(void)
{
  char buff[100];

  struct stats s = { { 1, 2 }, { 3 } };
  struct stats* stats = &s;

  sprintf(buff,"Index:1:%u:%u:%d\n",
              stats->connection.peer,
              stats->connection.local,
              stats->info.transferID);

  printf("  %s",buff);
  printf("  %d\n",stats->info.transferID);

  return 0;
}

你确定缓冲区足够大吗?是否确实使用了正确的类型说明符(
%u
%d
)?

此处没有足够的信息。请构造一个。请至少显示存储在
buff
stats
/
info
结构中的内容。请指定buff的大小。您应该始终选择
snprintf
而不是
sprintf
,以避免难以捕获的缓冲区溢出错误。例如,如果您的
buff
是编译时已知大小的char[]数组,请使用
snprintf(buff,sizeof buff,…)
我也尝试过snprintf,仍然是相同的错误这只是我6分钟前写的。唯一的可能性是,
buff
太小,无法保存所有打印的信息。前两个%u用于打印ip地址,下一个%d用于打印iperf id,该id是整数,
peer
local
的类型是什么?它们是无符号整数吗?如果是的话,bug肯定在其他地方,而不是这里。对等和本地是结构的地址,如果我对这两个结构都使用separate spritf,那么在C中就没有地址这样的东西。有整数、指针、结构、并集和数组。什么类型的?如果它们是指针,则应使用
%p
(例如
printf(“%p\n”,(void*)stats->connection.peer)
)将它们打印为指针。
  Index:1:1:2:3
  3