在文本文件中间附加字符串
Appending string in the middle of a text file
这是我的代码
#include <string>
#include <iostream>
#include <fstream>
int main(void){
fstream myfile2;
myfile2.open("test2.txt", ios::app);
string checkline;
getline(myfile2, checkline);
int razmer=checkline.length();
string balli="256";
myfile2.seekp(razmer);
myfile2<<balli;
}
test2.txt由2个字符串组成,所以它看起来像
Ivanov
Petrov
我想从Ivanov
到> Ivanov 256
。不碰第二根弦。但是我的代码根本不能工作。
编辑文本文件并不容易。通常的解决方案是将整个源文件读入内存,在内存中进行修改,然后将所有文件写出来。
在您的示例中,文件似乎是基于行的,您可以逐行读取它并将行放在std::vector
中。编辑要编辑的行,然后循环遍历vector并写出这些行。
注意:当写入文件时,您以写模式打开它,因此文件将被重新创建并丢失所有旧内容。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 递归形成字符串中所有数字字符的中间和?
- 如何组合一个宽字符字符串,中间插入一些空字符
- C++ 我想将两个字符串写入管道,中间有一小段延迟
- 在 txt 文件中搜索中间子字符串未被选中的字符串
- 如何从字符串中间开始的子字符串
- 从中间器转换字符串不起作用,出了什么问题?
- 在std::字符串或字符数组中间包含NULL字符的UPDATE
- 如何分辨 boost::split_iterator 从字符串中间开始
- 如何打印在中间包含0个字符的字符串
- 矢量字符串字符替换在字符串c++的中间
- 我怎么能在C++中的文件名中间使用字符串值呢
- 如何在序列化期间插入字符串流的中间
- 如何为字符串分配一个从数组中间开始的char数组
- 如何使用c++解析文本文件从中间到特定字符串
- 如何清除包含C函数声明的字符串中的注释和中间空白
- Ispunct()不删除字符串中间的逗号
- 在文本文件中间附加字符串
- 从文件中读取整数,中间有一个字符串
- 如何以特定格式将数字打印到字符串的中间