#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int c;
FILE *f;
const char fname[] = "inputdata";
printf("Opening \"%s\" for input in text mode.\n", fname);
if (!(f = fopen(fname, "r"))) {
fputs("fopen failed. Quitting.\n", stderr);
exit(EXIT_FAILURE);
}
printf("The characters read from the file are (in text mode):\n");
while ((c = fgetc(f)) != EOF) {
printf("%#04x %#05o %03d ", (unsigned) c, (unsigned) c, c);
if (iscntrl(c))
printf(" (a control character)\n");
else if (isspace(c))
printf(" (whitespace)\n");
else if (!isgraph(c))
printf(" (other non-graphic)\n");
else
printf("'%c'\n", c);
}
putchar('\n');
printf("Reopening \"%s\" for input in binary mode.\n", fname);
if (!(f = freopen(fname, "rb", f))) {
fputs("freopen failed. Quitting.\n", stderr);
exit(EXIT_FAILURE);
}
printf("The characters read from the file are (in binary mode):\n");
while ((c = fgetc(f)) != EOF) {
printf("%#04x %#05o %03d ", (unsigned) c, (unsigned) c, c);
if (iscntrl(c))
printf(" (a control character)\n");
else if (isspace(c))
printf(" (whitespace)\n");
else if (!isgraph(c))
printf(" (other non-graphic)\n");
else
printf("'%c'\n", c);
}
fclose(f);
return 0;
}
Many of Martin's posts are short, error-free, legible programs one can copy
and adapt easily. I get the same output he does for a different data set
on the same platform:
Opening "george.txt" for input in text mode.
The characters read from the file are (in text mode):
0x31 00061 049 '1'
0x20 00040 032 (whitespace)
0x20 00040 032 (whitespace)
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x31 00061 049 '1'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x31 00061 049 '1'
0x0a 00012 010 (a control character)
....
Reopening "george.txt" for input in binary mode.
The characters read from the file are (in binary mode):
0x31 00061 049 '1'
0x20 00040 032 (whitespace)
0x20 00040 032 (whitespace)
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x31 00061 049 '1'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x30 00060 048 '0'
0x31 00061 049 '1'
0x0d 00015 013 (a control character)
0x0a 00012 010 (a control character)
The tool that I have found very helpful for this type of work is od.exe
found here:
http://downloads.sourceforge.net/unxutils/UnxUtils.zip
Copy the .exe to a convenient directory and invoke it using the batch file
dump.bat. Dump.bat contains:
od -tx1 -Ax -v %1
-t == how to display data
x1 == one hex byte
-A == how to display address (offset from start of file)
x == hex
-v == show all data, including runs of duplicates
%1 first argument to .bat file
For example, if I have a file "chars.dat", then the appropriate command is:
C:\Users\epc\temp>dump chars.dat
--
George
When you turn your heart and your life over to Christ, when you accept
Christ as the savior, it changes your heart.
George W. Bush
Picture of the Day
http://apod.nasa.gov/apod/