Скопировать текстовый файл в новый файл, заменив в каждой строке сочетания цифр на два символа «z», и оставляя пустую строку после каждых двух строк пустоты. Написать на С++
Вот пример программы на C++, которая выполняет указанное действие:
#include <iostream> #include <fstream> #include <string> int main() { std::ifstream infile("input.txt"); std::ofstream outfile("output.txt"); if (!infile.is_open()) { std::cerr << "Error: unable to open input file" << std::endl; return 1; } if (!outfile.is_open()) { std::cerr << "Error: unable to create output file" << std::endl; return 1; } std::string line; int emptyLinesCount = 0; while (std::getline(infile, line)) { if (line.empty()) { emptyLinesCount++; if (emptyLinesCount == 2) { outfile << std::endl; emptyLinesCount = 0; } } else { for (char& c : line) { if (std::isdigit(c)) { c = 'z'; } } outfile << line << std::endl; } } infile.close(); outfile.close(); std::cout << "File has been successfully processed." << std::endl; return 0; }
Чтобы программа работала корректно, необходимо создать текстовый файл "input.txt" с исходным содержимым и запустить программу. Результирующий файл будет создан под именем "output.txt".
Вот пример программы на C++, которая выполняет указанное действие:
#include <iostream>#include <fstream>
#include <string>
int main() {
std::ifstream infile("input.txt");
std::ofstream outfile("output.txt");
if (!infile.is_open()) {
std::cerr << "Error: unable to open input file" << std::endl;
return 1;
}
if (!outfile.is_open()) {
std::cerr << "Error: unable to create output file" << std::endl;
return 1;
}
std::string line;
int emptyLinesCount = 0;
while (std::getline(infile, line)) {
if (line.empty()) {
emptyLinesCount++;
if (emptyLinesCount == 2) {
outfile << std::endl;
emptyLinesCount = 0;
}
} else {
for (char& c : line) {
if (std::isdigit(c)) {
c = 'z';
}
}
outfile << line << std::endl;
}
}
infile.close();
outfile.close();
std::cout << "File has been successfully processed." << std::endl;
return 0;
}
Чтобы программа работала корректно, необходимо создать текстовый файл "input.txt" с исходным содержимым и запустить программу. Результирующий файл будет создан под именем "output.txt".