ex04 handling error

This commit is contained in:
2025-09-07 19:45:09 +02:00
parent b5430df26b
commit 4be65d6b25

View File

@@ -6,7 +6,7 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */ /* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 14:44:18 by aortigos #+# #+# */ /* Created: 2025/09/06 14:44:18 by aortigos #+# #+# */
/* Updated: 2025/09/06 15:18:39 by aortigos ### ########.fr */ /* Updated: 2025/09/07 19:44:43 by aortigos ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -26,18 +26,23 @@ int main(int argc, char **argv)
s1 = argv[2]; s1 = argv[2];
s2 = argv[3]; s2 = argv[3];
line = ""; line = "";
std::ifstream infile(filename.c_str(), std::ifstream::in);
std::ofstream outfile((filename + ".replace").c_str());
if (!infile.is_open() || !outfile.is_open()) std::ifstream infile(filename.c_str(), std::ifstream::in);
if (!infile.is_open())
{ {
std::cout << "Ha ocurrido un error en los archivos" << std::endl; std::cout << "Ha ocurrido un error en el infile" << std::endl;
return (1);
}
std::ofstream outfile((filename + ".replace").c_str());
if(!outfile.is_open())
{
std::cout << "Hubo un error en el outfile" << std::endl;
return (1); return (1);
} }
while (std::getline(infile, line)) { while (std::getline(infile, line)) {
size_t pos = 0; size_t pos = 0;
while ((pos = line.find(s1, pos)) != std::string::npos) while ((pos = line.find(s1, pos)) != std::string::npos)
{ {
line.erase(pos, s1.length()); line.erase(pos, s1.length());
@@ -46,13 +51,10 @@ int main(int argc, char **argv)
} }
outfile << line << std::endl; outfile << line << std::endl;
} }
infile.close(); infile.close();
outfile.close(); outfile.close();
} else { } else {
std::cout << "Uso: ./sed <filename> <s1> <s2>" << std::endl; std::cout << "Uso: ./sed <filename> <s1> <s2>" << std::endl;
} }
return (0); return (0);
} }