From 87a2582eef4d6fcd514447119930defe5a76333f Mon Sep 17 00:00:00 2001 From: kinou-p Date: Tue, 21 Jun 2022 22:04:54 +0200 Subject: [PATCH] end 01 04 --- cpp01/ex04/Makefile | 35 +++++++++++++++++++++++++ cpp01/ex04/main.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++ cpp01/ex04/test1 | 18 +++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 cpp01/ex04/Makefile create mode 100644 cpp01/ex04/main.cpp create mode 100644 cpp01/ex04/test1 diff --git a/cpp01/ex04/Makefile b/cpp01/ex04/Makefile new file mode 100644 index 0000000..784caf7 --- /dev/null +++ b/cpp01/ex04/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/06/19 16:15:23 by apommier #+# #+# # +# Updated: 2022/06/19 16:15:32 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = a.out +SRCS = main.cpp\ + + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror +RM = rm -rf + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp01/ex04/main.cpp b/cpp01/ex04/main.cpp new file mode 100644 index 0000000..9aa18a0 --- /dev/null +++ b/cpp01/ex04/main.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/19 16:15:21 by apommier #+# #+# */ +/* Updated: 2022/06/21 22:02:37 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +# include +# include +# include +# include +# include + + +bool verifyPathname(std::string fileOne, std::string toFind, std::string newStr) +{ + if (!fileOne.length() && !toFind.length() && !newStr.length()) + { + std::cout << "Arguments must not be empty\n"; + return(0); + } + return (1); +} + +std::string changeLine(std::string buffer, std::string toFind, std::string newStr) +{ + int position = buffer.find(toFind); + + while (position != std::string::npos) + { + buffer.insert(position + toFind.length(), newStr); + buffer.erase(position, toFind.length()); + position = buffer.find(toFind); + } + return (buffer); +} + +int main(int ac, char **av) +{ + std::ifstream fileOne(av[1]); + std::ofstream newFile; + std::string allFile; + std::string buffer; + std::ofstream outfile((std::string)av[1] + ".replace"); + + if (ac != 4 || !verifyPathname(av[1], av[2], av[3]) || !fileOne) + { + std::cout << "Bad argument\n"; + return (0); + } + while (std::getline (fileOne,buffer)) + { + buffer = changeLine(buffer, av[2], av[3]); + allFile += buffer + '\n'; + } + outfile << allFile; +} \ No newline at end of file diff --git a/cpp01/ex04/test1 b/cpp01/ex04/test1 new file mode 100644 index 0000000..d3d4faf --- /dev/null +++ b/cpp01/ex04/test1 @@ -0,0 +1,18 @@ +awdakwflawfkaaw +fa +wd +aw +fawawawf + +a +wdawd +aw +aw +aw + + + +awaw + +awaw +awwa