C String in while-Schleife verketten

Ich versuche, einen Teil einer Struktur mit Hex-Werten zu verketten. Ich überfliege jedes Byte in der Schleife und wandle es in hex um. Dann möchte ich alles hex zu einer langen Zeichenkette verketten.

Am Ende der Schleife habe ich jedoch nur einen Wert. Aus irgendeinem Grund verkettet sich die Zeichenfolge nicht richtig. Irgendeine Idee, was ich falsch mache?

typedef struct OPTIONS_STR
{
    int max;
    int printName;
} OPTIONS;

void set_default_options(OPTIONS *options)
{
    options->max = -1;
    options->printName = 0;
}

void do_file(FILE *in, FILE *out, OPTIONS *options)
{
    char ch;
    int loop = 0;
    char buf[81];
    buf[0] = '\0';
    int sz1;
    int sz2;
    int sz3;

    int seeker = offsetof(struct myStruct, contents.datas);

    //find total length of file
    fseek(in, 0L, SEEK_END);
    sz1 = ftell(in);

    //find length from beggining to struct beginning and minus that from total length
    fseek(in, seeker, SEEK_SET);
    sz2 = sz1 - ftell(in);

    //set seek location at beginning of struct offset
    fseek(in, seeker, SEEK_SET);

    sz3 = sz2 + 1;
    char buffer[sz3];
    char msg[sz3];

    buffer[0] = '\0';

    while (loop < sz2)
    {
        if (loop == sz2)
        {
            break;
        }

        fread(&ch, 1, 1, in);
        sprintf(msg, "%02X", (ch & 0x00FF));
        strcpy(buffer, msg);

        ++loop;
    }
    printf("%s\n", buffer);
}

int main(int argc, const char * argv[]) {

    OPTIONS options;
    set_default_options(&options);

    const char *current = "/myfile.txt";
    FILE *f = fopen(current, "rb");
    do_file(f, stdout, &options);
    fclose(f);

};

Antworten auf die Frage(4)

Ihre Antwort auf die Frage