Created
December 1, 2015 23:08
-
-
Save lpar/a1f2b3bdafbc7d50d6a3 to your computer and use it in GitHub Desktop.
Somebody set us up the BOM!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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