2013-02-05 36 views
0

sprintf的是不是给了里边反变量stats-> info.transferID正确的值,但 printf的是该变量给出正确的价值观,所有其他值合适sprintf的不打印的最后一项

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); 

info是Transfer_Info类型的结构。

typedef struct Transfer_Info { 
    void *reserved_delay; 
    int transferID; 
    ---- 
    ---- 
} 

输出我得到:缓冲区

Index:1:2005729282:3623921856:0 

3 

大小足够多的保值,

在此先感谢

+4

这里没有足够的信息。请构建一个[最小测试用例](http://sscce.org)。 –

+4

请至少显示存入'buff'和'stats' /'info'结构中的内容 – Mike

+2

请指定buff的大小 – Riskhan

回答

1

工作对我来说:

#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; 
} 

输出( ideone):

Index:1:1:2:3 
    3 

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

+0

这正是我6分钟前写的。唯一的可能性,那就是'buff'太小而无法保存所有的打印信息。 – Andremoniy

+0

Alexey的巨大努力 – Riskhan

+0

前两个%u用于打印ip地址,接下来的%d应该打印整数的iperf id – kelvin