Hi; I've worked out all but 1 of the bugs in my first truly functional program in C.
The Bug:
Every time the fread() function in my program comes across a 0xFF character in the source file, it's feeding *something* outside the bounds of 0-255 into the corresponding unsigned char in my buffer array.
The fread call:
inBytes (defined earlier as unsigned char inBytes[1000]) is then passed into a switch:case function that tests each element of the array for values 0-255 inclusive. Characters are then converted to hexadecimal values & output into another file.
I can directly feed values 0-255 into elements of the inBytes array, and the output is perfect (no missed characters), but when fed from a file opened with:
The output was missing several characters.
Noticing the error in the output, I added a default: case to the switch that inserted '--' in any "uncaught" character; then my output file had proper spacing, and I was able to compare the output to a different (not written by me) conversion program. From the output compares, I found that only "FF" characters were affected. Each "FF" character put out from the other program corresponded exactly to an "--" from my program; so what it causing this non-numeric output from fread() for only that one character?
Obviously, I could alter my default: case to output "FF" instead of "--", but that doesn't fix the *cause* of my glitch, so I can't be certain it won't lead to other conversion errors in the future if I do it; much better to find, understand, then fix the original glitch, than to apply a sloppy workaround.
BTW: I'm compiling using gcc on an x86_64 computer running 64-bit OpenSuse Linux, if it matters (although I'm trying to write the program to be fully portable to other platforms).
The Bug:
Every time the fread() function in my program comes across a 0xFF character in the source file, it's feeding *something* outside the bounds of 0-255 into the corresponding unsigned char in my buffer array.
The fread call:
Code:
readLength = fread(inBytes, 1, 1000, inFile);
I can directly feed values 0-255 into elements of the inBytes array, and the output is perfect (no missed characters), but when fed from a file opened with:
Code:
inFile = fopen(fileName, "rb");
Noticing the error in the output, I added a default: case to the switch that inserted '--' in any "uncaught" character; then my output file had proper spacing, and I was able to compare the output to a different (not written by me) conversion program. From the output compares, I found that only "FF" characters were affected. Each "FF" character put out from the other program corresponded exactly to an "--" from my program; so what it causing this non-numeric output from fread() for only that one character?
Obviously, I could alter my default: case to output "FF" instead of "--", but that doesn't fix the *cause* of my glitch, so I can't be certain it won't lead to other conversion errors in the future if I do it; much better to find, understand, then fix the original glitch, than to apply a sloppy workaround.
BTW: I'm compiling using gcc on an x86_64 computer running 64-bit OpenSuse Linux, if it matters (although I'm trying to write the program to be fully portable to other platforms).