Original code used fwrite instead of putc.
What is expected is that the destination file will be written to. Instead I end up with a zero length file. I'm sure there is something simple I'm missing.
tia.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
int x = 0, freq = 0, cnt = 0;
FILE *fpt, *wpt;
char comment_block[2];
char bin;
if (argc != 5) {printf("Error: Please provide\n 1.) Source filename\n 2.) Destination filename\n 3.) Frequency for commitment\n 4.) Message to embed\n as arguments to %s.\n",argv[0]); exit(0);}
if ( (fpt = fopen(argv[1],"r")) == NULL || (wpt = fopen(argv[2],"a+")) == NULL) {printf("Error opening source or destination files.\n"); exit(0);}
freq = atoi(argv[3]);
while (fread(&comment_block,sizeof(comment_block),2,fpt) > 0) {
printf("At byte %d.\n",(x += 2));
if ((comment_block[0] & 0xFF) && (comment_block[1] & 0xD8)) {printf("Start of image found due to comment_block[0]: %p = %x and comment_block[1]: %p = %x\n",&comment_block[0],comment_block[0],&comment_block[1], comment_block[1]); x = 0; putc(comment_block[0],wpt); putc(comment_block[1],wpt); break;}
putc(comment_block[0],wpt); putc(comment_block[1],wpt);
}
while (fread(&bin,sizeof(bin),1,fpt) > 0) {
printf("Integer value for %p at character count %d = %x.\n",&bin,(x++),bin);
if (x > 1 && x % freq == 0 && cnt <= strlen(argv[4])) {bin = argv[4][cnt++]; printf("At character count: %d replacing to %x.\n",x,bin);}
putc(bin,wpt);
}
fclose(fpt); fclose(wpt);
return 0;
}