/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */ /* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include #include using std::string; using std::ifstream; using std::ofstream; int main(int argc, char **argv) { string text; string buffer; string filename; string oldStr; string newStr; int oldLen; int i = 0, fileLen = 0; if (argc != 4) return (EXIT_FAILURE); ifstream input(argv[1]); if (!input.is_open()) { std::cout << "Invalid file." << std::endl; return (EXIT_FAILURE); } oldStr = argv[2]; newStr = argv[3]; while (getline(input, buffer)) { text += buffer; text += '\n'; } input.close(); fileLen = text.length(); oldLen = oldStr.length(); while (i < fileLen) { i = text.find(oldStr, i); if (i == -1) break ; text.erase(i, oldLen); text.insert(i, newStr); } filename = argv[1]; ofstream output(filename.append(".replace")); output << text; output.close(); return (EXIT_SUCCESS); }