C语言文件读取
我是 C 新手,我面临的问题是我有一个已解压的二进制文件(即以某种格式)。
我要做的就是打包它并再次解压它,看看它是否与原始解压版本相同。
值得一提的一件事:我被告知打包(即转换为打包)和解包(即转换为解包)功能运行良好..只是想亲自确认一下并学习一点C...
我有两点我认为我犯了错误
1:我读取文件的方式
2:我没有正确考虑打包和解包的变量类型(即对于打包它是unsigned char *而对于解包它是short *)
int main(void) {
FILE *fp;
unsigned char* packed ;
short* unpacked;
size_t result;
int fileSize;
fp = fopen(FILENAME, "rb");
fseek (fp , 0 , SEEK_END);
fileSize = ftell (fp);
rewind (fp);
unpacked = (short*) malloc (sizeof(char)*fileSize);
result = fread(unpacked,1,fileSize,fp);
short *originalUnpacked = unpacked;
convert_to_packed(&unpacked, &packed);
convert_to_unpacked(&unpacked, &packed);
if (originalUnpacked == unpacked)
{
puts ("Thats it !!");
}
fclose(fp );
return EXIT_SUCCESS;
}
I am new to C and the problem i am facing is that that i have a binary file which is unpacked (i.e its in a certain format)..
What i am suppose to do is to pack it and unpack it again and see if its equal to the original unpacked version.
One thing worth mentioning: i have been told the packing (i.e convert to packed) and unpacking(i.e convert to unpacked) function works well.. Just want to confirm it for myself and learn a bit of C...
I have two points where i think i am doing mistake
1 : the way i am reading the file
2 : I am not properly considering the variable type of packed and unpacked (i.e for packed it is unsigned char * and for unpacked it is short * )
int main(void) {
FILE *fp;
unsigned char* packed ;
short* unpacked;
size_t result;
int fileSize;
fp = fopen(FILENAME, "rb");
fseek (fp , 0 , SEEK_END);
fileSize = ftell (fp);
rewind (fp);
unpacked = (short*) malloc (sizeof(char)*fileSize);
result = fread(unpacked,1,fileSize,fp);
short *originalUnpacked = unpacked;
convert_to_packed(&unpacked, &packed);
convert_to_unpacked(&unpacked, &packed);
if (originalUnpacked == unpacked)
{
puts ("Thats it !!");
}
fclose(fp );
return EXIT_SUCCESS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码非常糟糕。
当您应该比较内存时,您正在比较指针(
originalUnpacked == unpacked
)。为什么要混合使用short *
和unsigned char *
指针?如果数据是二进制“blob”,您可能应该只使用后者。要比较内存,请使用标准
memcmp()
函数。This code is very broken.
You're comparing pointers (
originalUnpacked == unpacked
) when you should be comparing memory. And why are you mixingshort *
andunsigned char *
pointers? If the data is a binary "blob", you should probably only use the latter.To compare memory, use the standard
memcmp()
function.您需要更好地理解指针是什么。将指针视为您放在字母上的地址,将数据或内存视为房屋。如果我复制地址,现在有两个字母,但只有一所房子,这两个字母都指同一所房子,我可以使用其中一个来到达该房子。现在,复制房子需要做更多的工作,而不仅仅是复制信上的地址。您需要一栋新房子和一封带有新地址的新信。
您将此地址提供给转换函数
convert_to_packed
和convert_to_unpacked
,然后它们会覆盖原始数据,因为您忘记建造新房子来保留原始数据。要修复代码,你需要建造一座新房子,并用它来存储装箱和拆箱的结果。然后,您需要比较两栋房子的内容,而不是比较它们的字母。
You need to get a better understanding of what pointers are. Think of a pointer as an address you'd put on a letter, and the data, or memory, as the house. If I make a copy of the address there are now two letters but only one house, both letters refer to the same house and I can use either to get to the house. Now, to duplicate the house requires a lot more work than just copying the address on the letter. You need a new house and a new letter with a new address.
You gave this address to the conversion functions
convert_to_packed
andconvert_to_unpacked
which then overwrote the original data because you forgot to build the new house to keep the original data in.To fix the code, you need to build a new house and use it to store the result of the packing and unpacking. You then need to compare the contents of the two houses and not the letters that address them.