Como gravar no meio de um arquivo em C ++?
Eu acho que isso deve ser bastante simples, mas meu Google não ajudou até agora ... Preciso gravar em um arquivo existente em C ++, mas não necessariamente no final do arquivo.
Eu sei que quando eu só quero acrescentar texto ao meu arquivo, posso passar a bandeiraios:app
ao ligaropen
no meu objeto de fluxo. No entanto, isso só permite escrever no final do arquivo, mas não no meio.
Fiz um pequeno programa para ilustrar a questão:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string path = "../test.csv";
fstream file;
file.open(path); // ios::in and ios::out by default
const int rows = 100;
for (int i = 0; i < rows; i++) {
file << i << "\n";
}
string line;
while (getline(file, line)) {
cout << "line: " << line << endl; // here I would like to append more text to certain rows
}
file.close();
}