相同的文件,相同的文件大小,但内存比较返回非零

发布于 2024-12-11 19:36:47 字数 649 浏览 0 评论 0原文

#define "/local/home/..."

FILE *fp;

short *originalUnPacked;
short *unPacked;

int fileSize;

fp = fopen(FILENAME, "r");
fseek (fp , 0 , SEEK_END);
fileSize = ftell (fp);
rewind (fp);

originalUnPacked = (short*) malloc (sizeof(char)*fileSize);
unPacked = (short*) malloc (sizeof(char)*fileSize);

fread(unPacked, 1, fileSize, fp);
fread(originalUnPacked, 1, fileSize, fp);    

if( memcmp( unPacked, originalUnPacked, fileSize) == 0) 
{
 print (" unpacked and original unpacked equal ") // Not happens
}

我对 C 的一点了解说应该打印最后一个 if 块中的打印语句,但它没有,任何想法为什么?

只是为了更加清晰并向您展示完整的代码,我在 if 块之前添加了一个 Define 语句和两个 fread 语句。

#define "/local/home/..."

FILE *fp;

short *originalUnPacked;
short *unPacked;

int fileSize;

fp = fopen(FILENAME, "r");
fseek (fp , 0 , SEEK_END);
fileSize = ftell (fp);
rewind (fp);

originalUnPacked = (short*) malloc (sizeof(char)*fileSize);
unPacked = (short*) malloc (sizeof(char)*fileSize);

fread(unPacked, 1, fileSize, fp);
fread(originalUnPacked, 1, fileSize, fp);    

if( memcmp( unPacked, originalUnPacked, fileSize) == 0) 
{
 print (" unpacked and original unpacked equal ") // Not happens
}

My little knowldege of C says that the print statement in the last if block should be printed but it doesnt, any ideas Why ??

Just to add more clarity and show you the complete code i have added a define statement and two fread statement before the if block.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

此岸叶落 2024-12-18 19:36:47

几点供您考虑:
1、ftell的返回类型 < code>long int 因此最好将 fileSize 声明为 long int (as sizeof(int) <= sizeof(长))。
2. 这是一个更好的实践C 不会对 malloc 的返回值进行类型转换。另外,在 malloc 中使用时,您可能可以摆脱 sizeof(char)
3. fread 从而推进文件流在第一次 fread 调用之后,文件流指针已前进了 fileSize 指定的文件大小。因此,紧随其后的第二个 fread 将无法读取任何内容(假设第一个成功)。 这就是您看到程序中提到的行为的原因。您需要在第二次调用 fread 之前使用 rewind 重置文件流指针。还可以查看fread的返回值,即成功读取的字节数,来查看实际成功读取了多少字节。尝试以下几行:

size_t bytes_read;
bytes_read = fread(unPacked, 1, fileSize, fp);
/* some check or print of bytes read successfully if needed */
/* Reset fp if fread was successfully to load file in memory pointed by originalUnPacked */
rewind(fp);
bytes_read = fread(originalUnPacked, 1, fileSize, fp);
/* some check or print of bytes read successfully if needed */
/* memcmp etc */

4. 检查 fopenmalloc 等的返回值以防止失败,即 NULL 可能是个好主意检查 fopen 的情况malloc
希望这有帮助!

Few points for your consideration:
1. The return type of ftell long int so it is better to declare fileSize as long int (as sizeof(int) <= sizeof(long)).
2. It is a better practice in C not to typecast the return value of malloc. Also you can probably get rid of sizeof(char) when using in malloc.
3. fread advances the file stream thus after the first fread call the file stream pointer has advanced by the size of the file as dictated by fileSize. Thus the second fread immediately after that will fail to read anything (assuming the first one succeeded). This is the reason why you are seeing the behavior mentioned in your program. You need to reset the file stream pointer using rewind before the second call to fread. Also you can check the return value of fread which is the number of bytes successfully read to check how many bytes were actually read successfully. Try something on these lines:

size_t bytes_read;
bytes_read = fread(unPacked, 1, fileSize, fp);
/* some check or print of bytes read successfully if needed */
/* Reset fp if fread was successfully to load file in memory pointed by originalUnPacked */
rewind(fp);
bytes_read = fread(originalUnPacked, 1, fileSize, fp);
/* some check or print of bytes read successfully if needed */
/* memcmp etc */

4. It may be a good idea to check for the return values of fopen, malloc etc against failure i.e. NULL check in case of fopen & malloc.
Hope this helps!

同展鸳鸯锦 2024-12-18 19:36:47

使用 malloc 分配的内存不是预先分配的初始化,因此它的内容是随机的,因此几乎可以肯定这两个分配是不同的。

预期的(概率上来说,“确定的”)结果正是所发生的。

您是否打算在使用 memcmp 进行测试之前将文件加载到这两个缓冲区中,但忘记这样做了?

The memory allocated with malloc is not pre-initialized, so its contents are random and thus almost certainly different for the two allocations.

The expected (probabilistically speaking, "certain") result is exactly what happens.

Did you mean to load the file into both of these buffers before testing with memcmp but forgot to do so?

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