kelvin
kelvin

Reputation: 149

sprintf is not printing the last entry

sprintf is not giving the proper value for tthe variable stats->info.transferID ,but printf is giving proper values for that variable ,all the other values are proper

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 is a structure of Transfer_Info type.

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

the output I'm getting:

Index:1:2005729282:3623921856:0

3

size of buffer is much enough to hold its value ,

Thanks in advance

Upvotes: 0

Views: 668

Answers (1)

Alexey Frunze
Alexey Frunze

Reputation: 62068

Works for me:

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

Output (ideone):

  Index:1:1:2:3
  3

Are you sure the buffer is big enough? Are you sure you are using correct type specifiers (%u and %d)?

Upvotes: 1

Related Questions