-1

I cant seem to use the following function to concatenate a string in the while loop. Any idea what Im doing wrong?

void do_file(FILE *in, FILE *out, OPTIONS *options)
{
    char ch;
    int loop = 0;
    int sz1,sz2,sz3;

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

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

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

    sz3 = (sz2 + 1) * 2;//Allocate enough size for 2x array length
    char buffer[sz3];//set size of buffer to be copied to
    char msg[sz3];//set size of msg

    buffer[0] = '\0';

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

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

        ++loop;
    }
    printf("%s\n", buffer);
}
3
  • why do you fseek(in, seeker, SEEK_SET) twice? Commented Sep 13, 2016 at 6:48
  • 6
    Repeating the post continuously with little (and wrong/bad) modification is not the way to solve your problems... Commented Sep 13, 2016 at 6:52
  • kfsone - good point, I will remove that Commented Sep 13, 2016 at 6:54

1 Answer 1

0

Your only strcat appends the char msg[sz3]; which is never filled.

EDIT The simplest fix would be to add

msg[1] = '\0';

after

char msg[sz3];//set size of msg

and to replace

fread(&ch, 1, 1, in);

with

fread(&msg[0], 1, 1, in);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi GMichael, I'm wondering how do I fill it. I know it probably sounds silly, but I've been trying to fill it for a long time and still cant figure it out, maybe you can help me?
@Ke. Comparing this to your older question...did you completely forget that you commented out your sprintf line that writes into msg?
I'm not really sure how that sprintf is supposed to work. I was commenting it out because I thought it was printing into the window for each loop, whereas I wanted it to print one loop at the end. Is the sprintf not really printing into the console then until the end?
printf and sprintf don't differ only by a s but work differently. As told you many times: look at the man!
@Ke Please see the update in the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.