From 0f3240844dbd5a874e1565a988ef355ededab6a5 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 13 Apr 2020 10:33:05 +0200 Subject: Fixing cpp01 --- cpp01/ex07/main.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'cpp01/ex07/main.cpp') diff --git a/cpp01/ex07/main.cpp b/cpp01/ex07/main.cpp index fbef95f..64e5ce8 100644 --- a/cpp01/ex07/main.cpp +++ b/cpp01/ex07/main.cpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/02 17:08:28 by cacharle #+# #+# */ -/* Updated: 2020/02/02 18:00:25 by cacharle ### ########.fr */ +/* Updated: 2020/04/13 10:31:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,17 @@ int main(int argc, char **argv) { if (argc != 4) { - std::cerr << "Usage: " << argv[0] << " filename s1 s2"; + std::cerr << "Usage: " << argv[0] << " filename s1 s2" << std::endl; return 1; } std::string filename(argv[1]); - std::string find_str(argv[2]); - std::string replace_str(argv[3]); + std::string s1(argv[2]); + std::string s2(argv[3]); + if (s1 == "" || s2 == "") + { + std::cerr << "Error: s1 and s2 should not be empty" << std::endl; + return 1; + } std::ifstream file(filename); std::ofstream outfile(filename + ".replace"); @@ -39,18 +44,20 @@ int main(int argc, char **argv) file.close(); return 1; } + std::string line; while (std::getline(file, line)) { while (true) { - size_t i = line.find(find_str); + size_t i = line.find(s1); if (i == std::string::npos) break; - line.replace(i, find_str.length(), replace_str); + line.replace(i, s1.length(), s2); } outfile << line << std::endl; } file.close(); outfile.close(); + return 0; } -- cgit