问题
I'm trying to find the byte length of two different files with the following code, but get the byte length as 1, which is obviously wrong.
In the long run, I'm trying to compare memory positions of each file and print out where they differ as you'll see. So I wasn't getting anywhere, and did printf
statements to see where the problem could be. Therefore, it looks as if my length isn't properly calculating.
Side note that may help with my issue - I found this for memcmp, but does this mean I can't use !=
?:
if Return value if < 0 then it indicates str1 is less than str2
if Return value if > 0 then it indicates str2 is less than str1
if Return value if = 0 then it indicates str1 is equal to str2
Help please!
void compare_two_binary_files(int f1, int f2)
{
ssize_t byte_read_f1, byte_read_f2, length, numRead, bob, length2;
char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
int count = 0, b_pos1, b_pos2;
while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof buf2) >0)) {
length = byte_read_f1;
length2 = byte_read_f2;
printf("F1 byte length:%o\n", length);
printf("F2 byte length:%o\n", length2);
ssize_t len = byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
b_pos1 = memcmp(buf1, buf2, len);
printf("Memcmp: %d\n", b_pos1);
if (memcmp(buf1, buf2, len) != 0){ // use memcmp for speed
ssize_t i;
for (i = 0; i<len; i++){
if (buf1[i] != buf2[i]) break;
}
}
回答1:
>
has a higher precedence than =
, therefore
if ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && ...)
is equivalent to
if (byte_read_f1 = (read(f1, buf1, sizeof buf1) > 0) && ...)
which assigns 1
to byte_read_f1
if at least one byte was read.
What you want is
if ((byte_read_f1 = read(f1, buf1, sizeof buf1)) > 0 && ...)
If your program reads from other than regular files (such as standard input) then you have also to consider the case that a read() returns less bytes than requested.
For regular files, read() always returns the number of bytes requested,
with the only exception at end-of-file of course. Therefore, if the files differ in length,
then at some point, read(f1, ...)
will return a different amount than read(f2, ...)
,
or one returns zero and the other not.
回答2:
I think there's a mistake in the while condition:
while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) &&
(byte_read_f2 = read(f2, buf2, sizeof buf2) >0))
where you assign read(f1, buf1, sizeof buf1) > 0
(the result is true = 1) to byte_read_f1
and so for byte_read_f2
.
Can this help you?
Update
How comments suggest:
The right form will be:
while (((byte_read_f1 = read(f1, buf1, sizeof buf1)) > 0) &&
((byte_read_f2 = read(f2, buf2, sizeof buf2)) >0))
回答3:
How about fseek()'ing to the end of each file, and querying the file position offset? The fstat() call or something like it might even tell you the size directly.
来源:https://stackoverflow.com/questions/21468435/using-file-i-o-to-read-byte-length