/* * my_sed(argc,argv) * filename = argv[1]; * s1 = argv[2]; * s2 = argv[3]; * input::ifstream ifs; * input = open(filename, std::ifstream::in); * * * * */ #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]); oldStr = argv[2]; newStr = argv[3]; while (getline(input, buffer)) text += buffer; 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); }