C Concatenate string in while loop -
this question has answer here:
- c concatenate string in while loop 2 answers
i cant seem use following function concatenate string in while loop. idea 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 struct beginning , minus total length fseek(in, seeker, seek_set); sz2 = sz1 - ftell(in); sz3 = (sz2 + 1) * 2;//allocate enough size 2x array length char buffer[sz3];//set size of buffer copied 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); }
your strcat
appends char msg[sz3];
never filled.
edit simplest fix add
msg[1] = '\0';
after
char msg[sz3];//set size of msg
and replace
fread(&ch, 1, 1, in);
with
fread(&msg[0], 1, 1, in);
Comments
Post a Comment