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. Node.js学习准备篇

    这里写个Node.js 准备篇包含内容有node.js 的安装,命令行运行node.js 文件,使用webStrom 编写 node.js 时有提示功能,并用webStrom 运行 Node.js 其 ...

  2. MySQL查询命令_SELECT 子查询

    首先创建一个table mysql> create table Total (id int AUTO_INCREMENT PRIMARY KEY,name char(20),stu_num in ...

  3. ACM总结——2017ACM-ICPC北京赛区现场赛总结

    现在距离比赛结束已经过了一个多星期了,也是终于有时间写下心得了.回来就是被压着做项目,也是够够的. 这次比赛一样是我和两个学弟(虽然是学弟,但我的实力才是最弱的T_T)一起参加的,成绩的话打铁,算是情 ...

  4. JS设计模式(5)发布订阅模式

    什么是发布订阅模式(观察者模式)? 定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 主要解决:一个对象状态改变给其他对象通知的问题,而且 ...

  5. Container的简单认识

    容器是一个标准的软件单元,它将代码及其所有依赖关系打包,以便应用程序从一个计算环境快速可靠地运行到另一个计算环境. Docker容器映像是一个轻量级,独立的可执行软件包,包含运行应用程序所需的一切:代 ...

  6. redux-thunk的理解

    问题:1.redux-thunk要解决什么问题? 要解决异步请求问题,Action发出以后,Reducer立即算出State,这叫做同步:Action发出以后,过一段时间再执行 Reducer,这就叫 ...

  7. 论文笔记:Visual Semantic Navigation Using Scene Priors

    Visual Semantic Navigation Using Scene Priors 2018-10-21 19:39:26 Paper:  https://arxiv.org/pdf/1810 ...

  8. 编码原则 之 Persistence Ignorance

    原文 The principle of Persistence Ignorance (PI) holds that classes modeling the business domain in a ...

  9. vue query或params传参

    1.query 传递端: this.$router.push({ path:"/AccountFP", query:{ id:row.id, userId:row.userId } ...

  10. [JavaScript-Function] Function Invocation/Call(函数调用) 以及call() and apply() 方法

    介绍:JS函数中的代码会被函数被invoke(调用)时执行. 函数被定义时代码不执行, 函数调用时函数内的代码会被执行. 常用的term是 call a function 而不是 invoke a f ...