The reason why I have written this post is because no other post on SO solves my problem. I've read all the posts on this subject and tried/tested the suggestions, but they dont work in the context of the code I posted - hence the post in the first place...
I have the following function and it give an extra loop at the end, adding another byte to the end of the string Im creating. I've tried adding -1 to the loop variable, but it nothing seems to get rid of this extra loop. Is feof the right choice for this exercise, or is fgetc better? If so, how can I use fgetc in the context of my function?
void do_file(FILE *in, FILE *out, OPTIONS *options)
{
char ch;
int loop = 0;
char buf[81];
buf[0] = '\0';
int seeker = offsetof(struct myStruct, contents.datas);
fseek(in, seeker, SEEK_SET);
fprintf(out, "%i", seeker);
while (options->max == -1 || loop < options->max)
{
if (feof(in))
{
break;
}
if (loop % 16 == 0)
{
if (strlen(buf) > 0)
{
buf[0] = '\0';
}
}
fread(&ch, 1, 1, in);
fprintf(out, "%02X", (int)(ch & 0x00FF));
if (isalnum(ch))
{
char tmp[2];
tmp[0] = ch;
tmp[1] = '\0';
strcat(buf, tmp);
}
loop++;
}
}