fread读文件读取不全解决方法

fread,对指定长度的文件数据。读取的长度远小于文件的总长度,怎么回事呢?

查MSDN,fopen最后一个参数:

t
Open in text (translated) mode.

In this mode, CTRL+Z is interpreted as an end-of-file character on input.

In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible.

This is done because using fseek and ftell to move within a file that ends with a CTRL+Z can cause fseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output.

When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters.

For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters.

b
Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

If t or b is not given in mode, the default translation mode is defined by the global variable _fmode.

If t or b is prefixed to the argument, the function fails and returns NULL.  

两种模式,默认应该是文本模式t,这个时候fread遇到控制字符就不往后读了。解决方法:

使用binnay mode 这样就能就没有上述问题了,一个简单的例子:

	FILE* pFile=fopen("fileName","rb");//这里加载一个PE文件
	fseek(pFile, 0, SEEK_END);
	int len = ftell(pFile);
	char* szBuf=new CHAR[len];
	memset(szBuf,0,len);
	fseek(pFile, 0, SEEK_SET);
	int iRead=fread_s(szBuf,len,1,len,pFile);


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值