Re: fgetc - end of line - where is 0xD?



On Sat, 06 Dec 2008 05:56:29 -0500, Martin Ambuhl wrote:

#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/
.



Relevant Pages

  • Re: Can somebody help...
    ... a number of unsafe constructs and non-standard extensions. ... That value is too large to portably store in an int. ... This results in undefined behavior because you lied to printf about what ...
    (comp.lang.cpp)
  • Re: Is this correct..??
    ... long int i; ... Almost everthing is defined in assembler. ... Easily defeated by compiler optimisations. ... printf and that the first assignment to i is never used and decides to ...
    (comp.lang.c)
  • Semaphores Block when they Shouldnt and Dont when they Should
    ... took the votes (encoded in the first character of the transmitted mes- ... int socketAccess; ... write(voteSocket, voteBuffer, 2); ... {printf("Couldn't make a socket.\n"); ...
    (comp.unix.programmer)
  • RSA encrypt/decrypt c program
    ... Encrypt or Decrypt? ... int i,number; ... printf;} ... scanf; ...
    (comp.programming)
  • Re: Inconsistent Program Results
    ... and adding ifabove the printf ... int main ... passing to strchr. ... Well, OK, if you know that char * and void * have the same internal ...
    (comp.lang.c)

Loading