Skip to content

Instantly share code, notes, and snippets.

@lpar
Created December 1, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpar/a1f2b3bdafbc7d50d6a3 to your computer and use it in GitHub Desktop.
Save lpar/a1f2b3bdafbc7d50d6a3 to your computer and use it in GitHub Desktop.
Somebody set us up the BOM!
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *ifp, *ofp;
ofp = fopen("test.txt", "wb");
if (ofp == NULL) {
printf("Can't open output file!\n");
exit(1);
}
fprintf(ofp, "This is a test\nTo see if there is a BOM.\n");
fclose(ofp);
ifp = fopen("test.txt", "rb");
if (ifp == NULL) {
printf("Can't open input file!\n");
exit(1);
}
unsigned char buffer[80];
size_t readnum = fread(buffer, sizeof(unsigned char), 80, ifp);
for (int i = 0; i < readnum; i++) {
int c = buffer[i];
if (c >= ' ' && c <= 'z') {
printf("%c", c);
} else {
printf("\n%#04x\n", c);
}
}
fclose(ifp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment