zoukankan      html  css  js  c++  java
  • C++ ifstream ofstream 注意事项

    很久没写C++,已经完全不会写了...

    在使用ifstream读取一个二进制文件时,发现读取的内容和源文件不相同,导致数据解析失败,于是尝试把用ifstream读取的内容用ofstream写入另一个文件来对比

    发现两个问题:

    1. ofstream在0x0A之前自动添加了0x0D,原因是打开方式未设定为std::ios_base::binary,导致把换行符LF改成了CR/LF

    Your stream is opening in text mode, and since 0x0A is the line feed (LF) character, that's being converted by your stream to 0x0D 0x0A, i.e. CR/LF.

    http://stackoverflow.com/questions/5173498/why-does-ofstream-insert-a-0x0d-byte-before-0x0a

    2.ifstream在读取到0x1A时自动触发EOF,此时,char数组里剩下空间全部填充为0xCD,导致读取内容不完整.0xCD为申请数组时new出来未用到空间,编译器会将这样的空间用0xCD填充

    于是改为使用fread去读文件

    http://blog.csdn.net/zssureqh/article/details/7684529

    http://bbs.csdn.net/topics/350140248

  • 相关阅读:
    delphi private public protected
    delphi 程序流程控制
    TTrayIcon-Delphi系统托盘组件
    如果没有你-莫文蔚
    ShellExecute 调用bat文件
    delphi ShellExecute使用
    delphi 测试ping
    centos7 安装redis
    my.cnf 基础配置
    Delphi的类和对象(七)- 继承
  • 原文地址:https://www.cnblogs.com/wmalloc/p/5647631.html
Copyright © 2011-2022 走看看