This commit is contained in:
Angel Ortigosa Perez
2025-09-07 10:03:17 +02:00
commit 859690063b
30 changed files with 846 additions and 0 deletions

26
ex04/Makefile Normal file
View File

@@ -0,0 +1,26 @@
NAME=sed
SRCS=main.cpp
OBJS=$(SRCS:.cpp=.o)
CC=g++
CFLAGS=-Wall -Wextra -Werror -std=c++98
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

58
ex04/main.cpp Normal file
View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/06 14:44:18 by aortigos #+# #+# */
/* Updated: 2025/09/06 15:18:39 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <fstream>
int main(int argc, char **argv)
{
std::string filename;
std::string s1;
std::string s2;
std::string line;
if (argc == 4)
{
filename = argv[1];
s1 = argv[2];
s2 = argv[3];
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::cout << "Ha ocurrido un error en los archivos" << std::endl;
return (1);
}
while (std::getline(infile, line)) {
size_t pos = 0;
while ((pos = line.find(s1, pos)) != std::string::npos)
{
line.erase(pos, s1.length());
line.insert(pos, s2);
pos += s2.length();
}
outfile << line << std::endl;
}
infile.close();
outfile.close();
} else {
std::cout << "Uso: ./sed <filename> <s1> <s2>" << std::endl;
}
return (0);
}