I need to remove all digits and spaces from a file but I want to do it without rewriting the file. I wrote the following code: `
#include <iostream>
#include <fstream>
#include <regex>
using namespace std;
int main() {
std::fstream fs("./input.txt", ios::in|ios::out);
string line;
while (getline(fs, line)) {
fs.seekp(-(1+(long)line.length()), ios::cur);
size_t line_length = line.length();
line = regex_replace(line, regex("\\d+"), "");
line = regex_replace(line, regex(" "), "");
char *writable = new char[line.length()];
std::copy(line.begin(), line.end(), writable);
fs.write(writable, line_length);
fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return 0;
}
` and it work but it leaves null-terminator \0 at the end of each line where there were spaces or numbers. I also tried deleting numbers and spaces character by character, using fstream::write("", 1), but instead of remove, \0 were written. Please explain how it works and how to fix it.