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 这样就能就没有上述问题了,一个简单的例子:

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

原文出自:http://blog.csdn.net/iamoyjj/article/details/6580978

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fread函数是用来从文件读取数据的,但是它并不会自动判断文件的大小。因此,如果你没有正确指定要读取的数据大小,fread可能会读取文件末尾,并且返回读取到的数据量小于你期望的大小。 要解决这个问题,你需要确保正确指定要读取的数据大小。通常,你可以使用ftell函数获取文件的当前位置,然后使用fseek函数将文件指针移动到文件的开头,然后再使用fread函数进行读取。 下面是一个示例代码,演示了如何正确使用fseek和ftell来读取整个文件: ```c #include <stdio.h> int main() { FILE *file = fopen("example.txt", "rb"); if (file == NULL) { printf("Failed to open file.\n"); return 1; } // 获取文件大小 fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); // 分配内存用于存储文件内容 char *buffer = (char *)malloc(size); if (buffer == NULL) { printf("Failed to allocate memory.\n"); fclose(file); return 1; } // 读取文件内容 size_t bytesRead = fread(buffer, 1, size, file); if (bytesRead != size) { printf("Failed to read file.\n"); free(buffer); fclose(file); return 1; } // 打印文件内容 printf("File content:\n%s\n", buffer); // 释放内存和关闭文件 free(buffer); fclose(file); return 0; } ``` 在这个示例中,我们首先使用fseek和ftell获取文件的大小,然后根据文件大小分配足够的内存用于存储文件内容。接下来,我们使用fread函数将文件内容读取到内存中。最后,我们打印文件内容,释放内存并关闭文件。 请注意,示例中的文件打开模式是"rb",表示以二进制模式读取文件。你可以根据需要修改打开模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值