我有一个名为 copynumbers.txt 的文本文件,我需要在使用时删除数字后面的一些数字示例将是一个包含以下内容的文本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
每个整数应占用 4 个字节空间。
我想删除或去掉数字 7 到 15,同时保留 1 到 6,然后添加数字 30。
所以文件将保留 1 到 6,并删除 7 到 15,然后我想在最后的 30 处。
我的新文件应该是这样的
1 2 3 4 5 6 30
我的问题是如何在不覆盖数字 1 到 6 的情况下做到这一点?因为当我使用
std::ofstream outfile;
outfile.open ("copynumbers.txt");
它将覆盖所有内容并在文件中只留下 30 个
当我使用时
ofstream outfile("copynumbers.txt", ios::app);
它只会在 15 之后追加 30,但不会删除任何内容。
我的一些代码:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outfile("copynumbers.txt", ios::app);
outfile.seekp(0, outfile.end);
int position = outfile.tellp();
cout << position;
//outfile.seekp(position - 35);
outfile.seekp(28);
outfile.write(" 30",4);
outfile.close();
return 0;
}