ex04: remove print and write to a file

This commit is contained in:
Camille Chauvet 2023-07-31 14:45:04 +02:00
parent efa91c0cb5
commit 42a06c46cd

View File

@ -8,17 +8,13 @@
static int get_text(const char *path, std::string &text) static int get_text(const char *path, std::string &text)
{ {
std::ifstream file(path); std::ifstream file(path);
std::string line;
if (!file.is_open()) if (!file.is_open())
{ {
std::cerr << "sed: file error" << std::endl; std::cerr << "sed: file error" << std::endl;
return (1); return (1);
} }
while (std::getline(file, line)) std::getline(file, text, '\0');
text += line + "\n";
if (text.size() != 0)
text.erase(text.size() - 1);
file.close(); file.close();
return (0); return (0);
} }
@ -60,7 +56,12 @@ int main(int ac, char **av)
} }
if (get_text(av[1], text)) if (get_text(av[1], text))
return (2); return (2);
std::cout << text << std::endl;
replaced = replace_text(text.c_str(), av[2], av[3]); 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;
} }