压缩数字:读取错误 [英] Compressing Numbers: Reading Error

查看:246
本文介绍了压缩数字:读取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此函数压缩数字:

I am using this function for compression of Numbers:

unsigned char *MyCompress(unsigned int num, unsigned char *buffer){
    int     i = 0;
    unsigned int r = num;
    unsigned char temp;
    unsigned char s[5];

    printf("received %d to compress\n", num);

    if(!r){
       *buffer++ = 0;
       return buffer;
    }

    while (r){
         s[i] = r & 127;
         r >>= 7;
         printf("s[%d]=%d; r=%d\n", i, s[i], r);
         i++;
    }
    while (--i >= 0){
         temp = (unsigned char)(s[i] | (i ? 128 : 0));
         printf("temp=%d\n", temp);
         *buffer++=temp;
    }
  return buffer;
}

//现在发送打印此号码

//Now sending to Print this number

 unsigned char *abc, *abc2;
 abc=(unsigned char *)malloc(10);
 abc2=abc;
 len=384;
 abc = MyCompress(len, abc);
 fwrite(abc2, 1, (abc-abc2), fp);

//输出

 s[0]=0; r=3;
 s[1]=3; r=0

 temp=131
 temp=0

现在读取时出错:

 unsigned int MyDeCompress(FILE *fp){
    unsigned int c;
    unsigned int num = 0;

    do{
       c = (unsigned) fgetc(fp);
       printf(Read="%d\n", c);
       if (c <= 0) {
          printf("c is < 0 in uncompress1()\n");
          exit(0);
       }
       num <<= 7;
       num |= c & 127;

       printf("uncompress: c = %d num = %d\n", c, num);

       if (!num)
          break;
    }while (c & 128);
    printf("Returning %d\n", num);
    return num;
 }

//输出

  Read=131
  uncompress: c = 3 num = 3
  Returning 3

为什么是返回3.如何重新获得384.
但其他数字正确读取。

Why it is Returning 3. How can I get again 384. But other numbers are reading correctly.

我想要的问题的逻辑解决方案。不是关于内存泄漏或如何读取文件或如何写入文件。

I want the logic solution of the problem. Not about Memory Leaks or How to Read File or How to Write File. Kindly provide solution to the problem what can be done.

推荐答案

你没有真正运行你在问题中发布的代码。发布代码是一件好事,但只有当它是你运行并且遇到问题的实际代码,并且如果你将它逐字切割并直接粘贴到问题中。

You did not actually run the code you posted in the question. Posting code is a good thing, but only if it is the actual code that you ran and are having a problem with, and if you cut and paste it verbatim, directly into the question.

一旦我把 Read = 放在它所属的引号里面,你的代码就不会做你所声称的。当你把131放进来时,它返回131。你没有问题。好了,至少不用131.

Once I put the Read= inside the quotes where it belongs, your code does not do what you claim it does. It returns 131 out when you put 131 in. You do not have a problem. Well, at least not with 131.

除了字符串之外,你的代码还有问题,除了他们的引号外。

There are problems with your code, other than strings that have strayed outside of their quotes.

This:

    if (c <= 0) {
        printf("c is <= 0 in uncompress1()\n");
        exit(0);
    }

如已经指出的, <= c / c $ c>中的 是无意义的,因为 c 是无符号的,但更重要的是,零是在编码数据中具有完全有效的值,例如每当该数字的低七位都是零时。数字128代码为129 0。

shouldn't be there at all. As was already pointed out, the < in the <= is pointless since c is unsigned, but more importantly, zero is a perfectly valid value to have in your coded data, e.g. whenever the low seven bits of the number are all zeros. The number 128 codes to 129 0.

对于零情况,应删除 if(!r){ c>

For the zero case, you should remove the if(!r){ section and simply make the loop do while instead of while.

当您从文件读取时,您需要查找文件结尾并处理它。

When you're reading from a file, you need to look for end of file and deal with it.

这篇关于压缩数字:读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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