簡體   English   中英

sprintf沒有打印最后一個條目

[英]sprintf is not printing the last entry

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

info是Transfer_Info類型的結構。

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

輸出( ideone ):

  Index:1:1:2:3
  3

您確定緩沖區足夠大嗎? 您確定使用正確的類型說明符( %u%d )嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM