0

I am writing a simple program that reads data from two files and performs a byte-by-byte comparison to find the first(if any) difference between the two.

Here is the code I'm having trouble with: it checks to see if the lengths of the files are different and prints appropriately:

len1 = read(dumpFile, file1, 1024);
len2 = read(fumpFile, file2, 1024);

if(len1<len2){
    printf("%d: EOF 0x%x\n", len1, file2[len1]);
    return;
}
if(len1>len2){
    printf("%d: 0x%x EOF\n", len2, file1[len2]);
    return;
}

Here's the thing, if the first conditional is triggered, the output is fine: 10: EOF 0x71

Triggering the second, oddly enough, produces weird output even when the same files are used in different places: 10: 0xfffffffe EOF

I can't see anything I could do to fix this; it should be pretty straightforward. Any ideas?

  • from [read](http://codewiki.wikidot.com/c:system-calls:read): return value: Returns the number of bytes that were read. If value is negative, then the system call returned an error. In two's complement 0xFFFFFFFE is -2. Are you sure there isn't an error elsewhere that's causing confusing downstream? – jellies Jun 06 '19 at 00:58
  • Change the declarations of `file` and `file2` from `char` to `unsigned char`. – Eric Postpischil Jun 06 '19 at 00:58

0 Answers0