相同的文件,相同的文件大小,但内存比较返回非零
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几点供您考虑:
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
的返回值,即成功读取的字节数,来查看实际成功读取了多少字节。尝试以下几行:4. 检查
fopen
、malloc
等的返回值以防止失败,即NULL
可能是个好主意检查fopen
的情况malloc
。希望这有帮助!
Few points for your consideration:
1. The return type of
ftell
long int
so it is better to declarefileSize
aslong 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 ofsizeof(char)
when using inmalloc
.3.
fread
advances the file stream thus after the firstfread
call the file stream pointer has advanced by the size of the file as dictated byfileSize
. Thus the secondfread
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 usingrewind
before the second call tofread
. Also you can check the return value offread
which is the number of bytes successfully read to check how many bytes were actually read successfully. Try something on these lines: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 offopen
&malloc
.Hope this helps!
使用
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?