zoukankan      html  css  js  c++  java
  • fread读文件读取不全解决方法

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

    查MSDN,fopen最后一个参数:

    tOpen 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.

    bOpen 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);

      



  • 相关阅读:
    k8s使用私有镜像仓库
    spark client 配置lzo
    jvm系列(四):jvm调优-命令篇
    mysqldump 备份还原数据库
    df 卡死及ls无法查看文件
    记录一次服务器断电,直接进入救援模式
    nginx开机自启脚本
    mongodb启动关闭脚本
    mongo数据备份恢复
    centos 快速配置网络
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132851.html
Copyright © 2011-2022 走看看