很久没写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

C++ ifstream ofstream 注意事项的更多相关文章

  1. C++ 输入输出文件流(ifstream&ofstream)

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O, ...

  2. c++ ifstream ofstream 文件流

    #include <fstream>ofstream //文件写操作 内存写入存储设备 ifstream //文件读操作,存储设备读区到内存中fstream //读写操作,对打开的文件可进 ...

  3. 从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态

    一.文件流 ofstream,由ostream派生而来,用于写文件 ifstream,由istream派生而来, 用于读文件 fstream,由iostream派生而来,用于读写文件 二.打开文件 说 ...

  4. C++ ifstream,ofstream读写二进制文件

    为什要吧数据存为二进制 这个嘛,是我个人习惯,一般,我们会把日志文件存为文本文件.数据文件存成二进制文件. 其实,我们接触的文件,比如图像.视频都是以二进制的形式存储的,要想查看这类数据,必须知道数据 ...

  5. C++ ifstream ofstream

    原文出自[比特网],转载请保留原文链接:http://soft.chinabyte.com/database/460/11433960.sh [导读] ofstream是从内存到硬盘,ifstream ...

  6. 文件流(fstream, ifstream, ofstream)的打开关闭、流状态

    一.文件流 ofstream,由ostream派生而来,用于写文件 ifstream,由istream派生而来, 用于读文件 fstream,由iostream派生而来,用于读写文件 二.打开文件 说 ...

  7. C++ 标准文件的写入读出(ifstream,ofstream)

    ttp://blog.csdn.net/a125930123/article/details/53542261     注: "<<", 插入器,向流输入数据     ...

  8. C++中的fstream,ifstream,oftream

    https://blog.csdn.net/kingstar158/article/details/6859379 先mark一个大佬的随笔,有时间再回头看 总结: 使用ifstream和ofstre ...

  9. c++ ofstream使用方法

    ofstream是从内存到硬盘,ifstream是从硬盘到内存,流缓冲即是内存空间. 插入器<<  : 向流输出数据. cout << "test!" &l ...

随机推荐

  1. python中关于shutdown 和closesocket的彻底理解!

    关于shutdown 和closesocket的彻底理解! shutdown 和closesocket 来,咱们彻底的来讨论一下这个shutdown 和closesocket 从函数调用上来分析(ms ...

  2. xshell6破解4窗口限制

    资源路径: 链接: https://pan.baidu.com/s/11zumNoaIN1AF_YhmiPGWdA 提取码: nfs9 破解方法 先卸载系统中已经安装的xshell与xftp 然后安装 ...

  3. LeetCode_167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...

  4. Mac 裁剪mp3

    系统自带的QuickTime Player

  5. css+html 实现 光晕 光圈

    <style> html { height: 100%;}body { height: 100%; background-color: #000; margin: 0; padding: ...

  6. iOS-AppDelegate详解

    项目中AppDelegate详解 1.AppDelegate.h //模板默认引入程序需要使用“类”的框架,即UIKit.h头文件,使它包含在程序中 #import <UIKit/UIKit.h ...

  7. php csv 简单的导入

    if($act == 'user_upload_do'){ global $db; $filename = $_FILES['file']['tmp_name']; if (empty ($filen ...

  8. jsp标签在spring boot中的关键用法

    <form:form modelAttribute="user" action="save" method="post" >// ...

  9. Hbuilder连接苹果手机

    最简单的连接方法 1     手机电脑连接---usb 2     pc下载iTunes 3     信任电脑,手机信任设备(设置--->通用--->设备管理-->信任) 4     ...

  10. AD域环境搭建

    1.安装Windows server服务器 我安装的是Windows server 2012 Standard x64 下载地址:https://pan.baidu.com/s/1dZ_B5JIEit ...