From 42a06c46cda61b061edb54bd140ac960068cc3e7 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Mon, 31 Jul 2023 14:45:04 +0200 Subject: [PATCH] ex04: remove print and write to a file --- ex04/src/main.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ex04/src/main.cpp b/ex04/src/main.cpp index 0fc39bd..ef2282e 100644 --- a/ex04/src/main.cpp +++ b/ex04/src/main.cpp @@ -8,17 +8,13 @@ static int get_text(const char *path, std::string &text) { std::ifstream file(path); - std::string line; if (!file.is_open()) { std::cerr << "sed: file error" << std::endl; return (1); } - while (std::getline(file, line)) - text += line + "\n"; - if (text.size() != 0) - text.erase(text.size() - 1); + std::getline(file, text, '\0'); file.close(); return (0); } @@ -60,7 +56,12 @@ int main(int ac, char **av) } if (get_text(av[1], text)) return (2); - std::cout << text << std::endl; replaced = replace_text(text.c_str(), av[2], av[3]); - std::cout << replaced << std::endl; + std::ofstream file((std::string(av[1]) + std::string(".replace")).c_str()); + if (!file.is_open()) + { + std::cerr << "sed: file error" << std::endl; + return (1); + } + file << replaced << std::endl; }