Skip to content

Instantly share code, notes, and snippets.

@veapon
Created October 27, 2014 14:49
Show Gist options
  • Save veapon/b3efb7b91739b112aef6 to your computer and use it in GitHub Desktop.
Save veapon/b3efb7b91739b112aef6 to your computer and use it in GitHub Desktop.
binread
FILE *fin, *fout;
char *buf;
size_t len;
if (fopen_s(&fin, "1.jpg", "rb") != 0) {
printf("Error occured while opening 1.jpg.\n");
return 0;
}
fseek(fin, 0, SEEK_END);
len = ftell(fin);
fseek(fin, 0, SEEK_SET);
printf("File size: %d\n", len);
if ((buf = (char *)malloc(len)) == NULL) {
printf("Error occured while malloc memory.\n");
return 0;
}
fread(buf, sizeof(char), len, fin);
fclose(fin);
printf("buf: %s\n", buf);
// write
if (fopen_s(&fout, "1.out.jpg", "wb") != 0) {
printf("Error occured while opening 1.out.jpg.\n");
return 0;
}
fputs(buf, fout);
fclose(fout);
free(buf);
while (1) {}
return 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment