使用文件I / O读取字节长度 [英] Using file i/o to read byte length

查看:146
本文介绍了使用文件I / O读取字节长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到两个不同的文件的字节长度用下面的code,但得到的字节长度为1,这显然是错误的。
从长远来看,我想比较每个文件的存储位置并打印出它们的不同,你会看到的。所以,我没有成气候,并没有的printf 语句,看看那里的问题可能是。因此,它看起来好像我的长度不正确计算。

旁注可能与我的问题帮助 - 我发现这对memcmp,但这是否意味着我不能使用 =

!?

如果返回值,如果< 0则表明str1和小于str2的

如果如果> 0,则表明STR2返回值小于STR1

如果如果= 0,则表明STR1返回值等于STR2

请帮助!

 无效compare_two_binary_files(INT F1,F2 INT)
 {
         ssiz​​e_t供byte_read_f1,byte_read_f2,长度,numRead,鲍勃,长度;
         炭BUF1 [BUF_SIZE],BUF2 [BUF_SIZE],[100],B [100],计数器[100];
         诠释计数= 0,b_pos1,b_pos2;
         而((byte_read_f1 =读(F1,BUF1,sizeof的BUF1)大于0)及及(byte_read_f2 =读(F2,BUF2,sizeof的BUF2)0)){
                 长度= byte_read_f1;
                 长度2 = byte_read_f2;
                 的printf(F1字节长度:%ο\\ n,长度);
                 的printf(F2字节长度:%ο\\ n,长度);
                 ssiz​​e_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);
                 如果(memcmp(BUF1,BUF2,LEN)!= 0){//使用memcmp速度
                         ssiz​​e_t供我;
                         对于(i = 0; I< LEN,我++){
                                 如果(BUF1 [I] = BUF2 [I]!)打破;
                         }
 }


解决方案

> 具有更高的precedence比 = ,因此

  IF((byte_read_f1 =读(F1,BUF1,sizeof的BUF1)大于0)及和放大器; ...)

等同于

 如果(byte_read_f1 =(读(F1,BUF1,sizeof的BUF1)大于0)及和放大器; ...)

这指定 1 byte_read_f1 如果至少有一个字节被读取。

您需要的是

  IF((byte_read_f1 =读(F1,BUF1,sizeof的BUF1))大于0和放大器;&安培; ...)

如果你的程序比其他常规文件(如标准输入)读取
那么你还认为这是一个read()返回比请求更少的字节的情况。

有关普通文件,阅读()总是返回请求的字节数,
与档案结尾当然是唯一的例外。因此,如果文件的长度不同,
那么在某些时候,阅读(F1,...)将返回不同的金额超过阅读(F2,...)
或者一个返回零和其他没有。

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;
                         }
 }

解决方案

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

这篇关于使用文件I / O读取字节长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆