When I use binary and out mode to open a exist file, and to modify the 4th and 8th byte data to 0x78, I found that the whole file will be rewrite, like below:

#include <fstream>
#include <iostream>
using namespace std; int main(int argc, char **argv)
{
fstream out;
out.open("./test.txt", ios_base::binary | ios_base::out); uint8_t data = 0x78; out.seekp(4, ios_base::beg);
out.write((char *)&data, 1); out.seekp(8, ios_base::beg);
out.write((char *)&data, 1); out.close();
return 0;
}

  And the file original data is :

FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

  But the file modified is:

00 00 00 00 78 00 00 00 78

  I have tried ios::app and ios::ate, the ios::app will out put my data at the end of the file and the skeep do not work.

  Finally, I thought out that the file is just a out file, so the api will not see the original data of the file, so I should add the ios::in.

#include <fstream>
#include <iostream>
using namespace std; int main(int argc, char **argv)
{
fstream out;
out.open("./test.txt", ios_base::binary | ios_base::out | ios_base::in); uint8_t data = 0x78; out.seekp(4, ios_base::beg);
out.write((char *)&data, 1); out.seekp(8, ios_base::beg);
out.write((char *)&data, 1); out.close();
return 0;
}

  

  And finally, I got the right data.

FF FF FF FF 78 FF FF FF 78 FF FF FF FF FF FF FF

  

binary and out mode to open a file的更多相关文章

  1. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:

    从库报这个错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first lo ...

  2. mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理

    年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...

  3. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

    setup slave from backup i got error Got fatal error 1236 from master when reading data from binary l ...

  4. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列三:重置主从同步

    1:停止slave服务器的主从同步 stop slave; 2:对Master数据库加锁 flush tables with read lock; 3:备份Master上的数据 mysqldump - ...

  5. 主从同步遇到 Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'时怎么解决

    首先遇到这个是因为binlog位置索引处的问题,不要reset slave: reset slave会将主从同步的文件以及位置恢复到初始状态,一开始没有数据还好,有数据的话,相当于重新开始同步,可能会 ...

  6. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列二:reset slave

    reset slave会清除从库的所有复制信息.一般应用场景:如切换为不同的Master, 主从重做等: 1. 命令在slave上执行,执行前一定要stop slave. 2. 执行reset sla ...

  7. 'Could not find first log file name in binary log index file'的解决办法

    数据库主从出错: Slave_IO_Running: No 一方面原因是因为网络通信的问题也有可能是日志读取错误的问题.以下是日志出错问题的解决方案: Last_IO_Error: Got fatal ...

  8. Serialize and Deserialize Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  9. [LintCode] Binary Tree Serialization

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

随机推荐

  1. web api使用JObject接收时,报“无法创建抽象类”错误

    https://bbs.csdn.net/topics/391952288 在下列函数中增加  ModelBinders.Binders.Add(typeof(JObject), new JObjec ...

  2. 记账本微信小程序开发四

    学习添加组件 集成日期组件 添加组件 需要在main.js文件中,声明一个data值date与wxml中的{{date}}绑定关联,然后在onLoad中初始化字符串格式的日期值, 处理日期组件点击确认 ...

  3. JavaScript Dom 查找

    JavaScript Dom 查找 一.直接查找 获取单个元素 document.getElementById('i1') 获取多个元素(列表数组) document.getElementsByTag ...

  4. 剑指offer(5)用两个栈实现队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目分析 栈是先进后出,队列是先进先出,因此两个栈,一个用来push,一个用来pop,同时注意下两个栈不 ...

  5. 剑指offer(11)二进制中1的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 题目分析 首先我们要了解计算机中负数使用补码表示的,原码.补码的概念以及原理可以参考这里,这个题目我们应该从二进制入手,值得 ...

  6. python scrapy baidu image【转】

    原 https://github.com/vivianLL/baidupictures #!/usr/bin/env Python # coding=utf-8 #__author__ = 'leil ...

  7. Always clear download 下载 谷歌浏览器插件

    由于该博文不支持上传压缩包,因此,如有需要always clear download插件的可点击此链接在百度网盘上下载https://pan.baidu.com/s/13wWchis3iKqXkIA5 ...

  8. spring(aop面向切面编程)

    aop很早有研究过,但是最近想回顾下,顺便记录下,aop的优点有很多,实用性也很广,就好比最早在公司没有使用aop的时候没个业务层都要写try catch来捕获异常,来处理异常,甚至于记录异常或者日志 ...

  9. python 获取subprocess进程执行后返回值

    test.py #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr= ...

  10. windows下如何批量修改文件名

    windows下如何批量修改文件名 一.总结 一句话总结:就是用excel生成了多条修改文件名的dos命令然后执行,比较核心的就是把图片名称导入excel 将图片名称导入excel---编写如下DOS ...