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. Dom4j基础

    dom4j是一个非常非常优秀的Java XML API,用来读写XML文件,具有性能优异.功能强大和易于使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.对主流的Jav ...

  2. 网页分享到微博、QQ、QQ空间、微信

    <ul id="content-share-list" class="bdsharebuttonbox bdshare-button-style0-16" ...

  3. Python imprt动态模块

    1.解释器内部用的动态导入 directory_variable = __improt__("directory.filename") print(directory_variab ...

  4. Python socket粘包解决

    socket粘包: socket 交互send时,连续处理多个send时会出现粘包,soket会把两条send作为一条send强制发送,会粘在一起. send发送会根据recv定义的数值发送一个固定的 ...

  5. mysql5.6 centos编译部署

    准备工作 创建用户 useradd mysql 删除老版本 rpm -qa |grep mysql rep -e mysql包 重命名默认的mysql配置文件 mv /etc/my.cnf /etc/ ...

  6. rabbitmq 配置集群镜像

  7. windows升级node

    之前用的node版本太低,不兼容webpack4.x,需要升级,网上搜索了许多方法,发现在windows下行不通 找到的教程都说全局安装node下的一个名为n的模块,这个模块是node专门用于版本管理 ...

  8. Lintcode469-Same Tree-Easy

    469. Same Tree Check if two binary trees are identical. Identical means the two binary trees have th ...

  9. 论Object.keys(), Object.getOwnPropertyNames(), for in, Object.getOwnPropertySymbol()区别

    前不久,一朋友求助,让我给解释一波Object.keys(), Object.getOwnPropertyNames(), for in的区别,面试中好几次呗问了.所以,抽了点时间看了看,大概把我看的 ...

  10. Java 中断

    https://zhuanlan.zhihu.com/p/45667127 看的似懂非懂