压缩算法 [英] compression algorithm

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

问题描述

我正在开发一种压缩算法,其中我们必须在C中编写代码。程序需要一个文件,并删除每个字符中的最高有效位,并将压缩文本存储在另一个文件中。我写了一个名为compress的函数,如下所示。我得到一个故障,而释放out_buf。任何帮助将是一个非常愉快。

I'm working on a compression algorithm wherein we have to write code in C. The program takes a file and removes the most significant bit in every character and stores the compressed text in another file. I wrote a function called compress as shown below. I'm getting a seg fault while freeing out_buf. Any help will be a great pleasure.

推荐答案

您关闭out_fd两次,因此第二次它是一个无效的文件描述符。但更重要的是,您需要检查您使用sizeof(),这与查找动态分配的缓冲区的大小不同(sizeof返回指针的大小,而不是缓冲区)。你不显示调用代码,但使用strcat()在缓冲区传入一直是值得一看(调用程序通过的缓冲区足够大的结果?)。

You close out_fd twice, so of course the second time it is an invalid file descriptor. But more than that, you need to review your use of sizeof() which is NOT the same as finding the buffer size of a dynamically-allocated buffer (sizeof returns the size of the pointer, not the buffer). You don't show the calling code, but using strcat() on a buffer passed-in is always worth a look too (is the buffer passed by the caller large enough for the result?).

无论如何,这应该足以让你再次去...

Anyway, that should be enough to get you going again...

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

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