You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.7 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */
/* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <fstream>
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);
}