end 01 04

This commit is contained in:
kinou-p 2022-06-21 22:04:54 +02:00
parent e81653bf5d
commit 87a2582eef
3 changed files with 115 additions and 0 deletions

35
cpp01/ex04/Makefile Normal file
View File

@ -0,0 +1,35 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

62
cpp01/ex04/main.cpp Normal file
View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/19 16:15:21 by apommier #+# #+# */
/* Updated: 2022/06/21 22:02:37 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
# include <iostream>
# include <sys/stat.h>
# include <fstream>
# include <sstream>
# include <string>
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;
}

18
cpp01/ex04/test1 Normal file
View File

@ -0,0 +1,18 @@
awdakwflawfkaaw
fa
wd
aw
fawawawf
a
wdawd
aw
aw
aw
awaw
awaw
awwa