C语言文件读取

发布于 2024-12-11 14:37:34 字数 810 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-12-18 14:37:34

这段代码非常糟糕。

当您应该比较内存时,您正在比较指针(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 mixing short * and unsigned char * pointers? If the data is a binary "blob", you should probably only use the latter.

To compare memory, use the standard memcmp() function.

随波逐流 2024-12-18 14:37:34

您需要更好地理解指针是什么。将指针视为您放在字母上的地址,将数据或内存视为房屋。如果我复制地址,现在有两个字母,但只有一所房子,这两个字母都指同一所房子,我可以使用其中一个来到达该房子。现在,复制房子需要做更多的工作,而不仅仅是复制信上的地址。您需要一栋新房子和一封带有新地址的新信。

unpacked = (short*) malloc (sizeof(char)*fileSize);
  ^^                  ^^
the letter          building the house


short *originalUnpacked = unpacked;
         ^^                  ^^
       new letter         old letter = copying the letter (address) not the house!


if (originalUnpacked == unpacked)
        ^^                 ^^
    new letter          old letter - neither changed so are equal (same address)

您将此地址提供给转换函数 convert_to_packedconvert_to_unpacked,然后它们会覆盖原始数据,因为您忘记建造新房子来保留原始数据。

要修复代码,你需要建造一座新房子,并用它来存储装箱和拆箱的结果。然后,您需要比较两栋房子的内容,而不是比较它们的字母。

 short newUnpacked = (short*) malloc (sizeof(char)*fileSize);

 convert_to_packed(&unpacked, &packed);
 convert_to_unpacked(&unpacked, &newUnpacked); // put unpacked data into new house!

 if (memcmp (unpacked, newUnpacked, filesize) == 0) // comparing contents of houses!

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.

unpacked = (short*) malloc (sizeof(char)*fileSize);
  ^^                  ^^
the letter          building the house


short *originalUnpacked = unpacked;
         ^^                  ^^
       new letter         old letter = copying the letter (address) not the house!


if (originalUnpacked == unpacked)
        ^^                 ^^
    new letter          old letter - neither changed so are equal (same address)

You gave this address to the conversion functions convert_to_packed and convert_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.

 short newUnpacked = (short*) malloc (sizeof(char)*fileSize);

 convert_to_packed(&unpacked, &packed);
 convert_to_unpacked(&unpacked, &newUnpacked); // put unpacked data into new house!

 if (memcmp (unpacked, newUnpacked, filesize) == 0) // comparing contents of houses!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文