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. Spring Boot IoC 容器初始化过程

    1. 加载 ApplicationContextInializer & ApplicationListener 2. 初始化环境 ConfigurableEnvironment & 加 ...

  2. Bootstrap日期插件在线使用,引入即可

      引入部分 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script& ...

  3. javadate相关

    /** * 根据年月获取对应的天数 */ int getDaysByYearMonth(int year, int month) { Calendar a = Calendar.getInstance ...

  4. K8S学习笔记之Kubernetes 部署策略详解

    0x00 概述 在Kubernetes中有几种不同的方式发布应用,所以为了让应用在升级期间依然平稳提供服务,选择一个正确的发布策略就非常重要了. 选择正确的部署策略是要依赖于我们的业务需求的,下面我们 ...

  5. Python 面向对象介绍

    面向对象,面向过程 面向对象引子 人狗大战,人与狗都有不同的特点,如果要写出这两个不同角色 需要写出两个角色,可以使用嵌套函数,函数内在写入函数,然后通 过字典,将里层函数reture出来,在调用. ...

  6. Python 堡垒机介绍

    堡垒机说明 由于运维行业流动性很高,也为了防止有人在服务中残留后门,照成安全隐患,在这里我们使用堡垒机保证服务器管理安全. 我们知道运维人员在登陆服务时需要登陆用户,从客户端到服务端的过程中堡垒机,将 ...

  7. GDB in Action

    GDB in Action 入门 编译 gcc -g -O0 -o word2vec.c word2vec -g 选项:要求 gcc 编译器保留调试符号信息. -O0 选项表示不优化,从 O1 ~ O ...

  8. D6差分及树上差分

    原谅我这篇博客拖了很久才写: 来到学校就和白痴一样缺了一世纪的课 上课特别懵:还有开学考枯了: 差分有列的差分,对于一段区间[l,r]进行修改,显然如果我们对于他的差分数组的l和r+1进行修改就可以了 ...

  9. topcoder srm 585 div1

    problem1 link 最优的策略就是从最低下一层开始,每两层的三个节点的子树都可以用一次遍历覆盖. problem2 link 从大到小依次放置每一种数字,并记录已经放置的数字一共有多少个$m$ ...

  10. 掌握闭包closure (含义及优缺点)

    个人认为闭包其实非常好理解,我们一起去认识什么是闭包. 在javascript脚本语言中,变量的作用域只有两种,一种是全局变量,一种是局部变量. 全局变量的函数可以在整个javascript脚本语言中 ...