简体   繁体   English

使用文件i / o读取字节长度

[英]Using file i/o to read byte length

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. 我试图用以下代码找到两个不同文件的字节长度,但得到字节长度为1,这显然是错误的。 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. 所以我没有到达任何地方,并做了printf语句,看看问题可能在哪里。 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 != ?: 可能有助于我的问题的旁注 - 我发现这是为memcmp,但这是否意味着我不能使用!= ?:

if Return value if < 0 then it indicates str1 is less than str2 如果返回值<0则表示str1小于str2

if Return value if > 0 then it indicates str2 is less than str1 如果返回值> 0则表示str2小于str1

if Return value if = 0 then it indicates str1 is equal to str2 如果返回值if = 0则表示str1等于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;
                         }
 }

I think there's a mistake in the while condition: 我认为在while条件下有一个错误:

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 . 其中分配read(f1, buf1, sizeof buf1) > 0 (结果为真= 1)以byte_read_f1因此对于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))

> 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. 如果至少读取了一个字节, byte_read_f1 1分配给byte_read_f1

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. 如果您的程序从常规文件以外的其他文件(例如标准输入)读取,那么您还必须考虑read()返回的字节数少于请求的情况。

For regular files, read() always returns the number of bytes requested, with the only exception at end-of-file of course. 对于常规文件,read()始终返回请求的字节数,当然只有例外的文件结尾处。 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. 因此,如果文件的长度不同,那么在某些时候, read(f1, ...)将返回与read(f2, ...)不同的量,或者一个返回零而另一个不返回。

How about fseek()'ing to the end of each file, and querying the file position offset? 如果fseek()到每个文件的末尾,并查询文件位置偏移量? The fstat() call or something like it might even tell you the size directly. fstat()调用或类似的东西甚至可以直接告诉你大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM