Need to convert fprintf to print to buffer

  asprintf, c++, printf

I have a simple loop to print an array directly to file:

   for (uint32_t i = 0; i < 256; i++) {
        fprintf(m_file, "%c%c%c%c", i, i, i, 0);
    }

I would like to convert this to print the array to a buffer, and then write to file in a single call

    size_t ind = 0;
    uint8_t buf[1024];
    for (uint32_t i = 0; i < 256; i++) {
        sprintf((char*)(buf+ind),"%c%c%c%c",i,i,i,i);
        ind +=4;
    }
    fwrite(buf, 1, 1024,m_file);

But this gives a different result. What am I missing here:

Source: Windows Questions C++

LEAVE A COMMENT