From: Dominik Kaiser Date: Wed, 12 Feb 2025 09:32:31 +0000 (+0100) Subject: Add implementation X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=dea263aa0b0b03b59064e6b879d1fdbe6c0e441e;p=42%2Fcpp01.git Add implementation --- diff --git a/ex04/main.cpp b/ex04/main.cpp index 7dae41e..f2cbb8d 100644 --- a/ex04/main.cpp +++ b/ex04/main.cpp @@ -6,9 +6,53 @@ /* By: dkaiser +#include +#include +#include + +static void replace(const std::string &filename, + const std::string &find_str, + const std::string &replace_str) +{ + std::ifstream infile(filename); + std::ofstream outfile(filename + ".replace"); + std::string line; + std::size_t find_pos; + std::size_t search_from; + + while (std::getline(infile, line)) + { + find_pos = 0; + search_from = 0; + while (find_pos != std::string::npos) + { + find_pos = line.find(find_str, search_from); + if (find_pos == std::string::npos) + { + break; + } + outfile << line.substr(search_from, find_pos); + outfile << replace_str; + search_from = find_pos + find_str.length(); + } + outfile << line.substr(search_from) << std::endl; + } +} + int main(int argc, char *argv[]) -{} +{ + if (argc != 4) + { + std::cerr << "Expected args: "; + std::cerr << std::endl; + return 1; + } + replace(argv[1], argv[2], argv[3]); +} + +