替换文本中的字符串

时间:2021-08-25 18:03:43   收藏:0   阅读:7

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main() {
ifstream in("a.txt"); // 打开foo.txt
stringstream ss;
ss << in.rdbuf(); // 将文件整个buffer放到stringstream中
string s;
s = ss.str(); // 把ss整个作为一个string放入s,文件所有内容就在s中

// ... 操作s
s.replace(78, 11, "192.168.4.2"); //用s1替换s中从pos开始(包括0)的n个字符的子串

// 把s重新写回文件
in.close();
ofstream out("a.txt");
out << s;
// 不需要out.close(),scope结束out析构会自动close()

原文:https://www.cnblogs.com/wang-xiao-shuai/p/15184685.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!