2013-02-05 39 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