update: finishing the ex04 of the module 01

This commit is contained in:
Raphael 2024-11-06 16:14:44 +01:00
parent 08a23cc42b
commit afd6bd90af
3 changed files with 30 additions and 12 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 15:24:29 by rparodi #+# #+# */ /* Created: 2024/11/05 15:24:29 by rparodi #+# #+# */
/* Updated: 2024/11/05 22:02:35 by rparodi ### ########.fr */ /* Updated: 2024/11/06 16:09:06 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,6 +27,12 @@ int main(int argc, char *argv[])
return (1); return (1);
} }
Sed new_sed = Sed(argv, &input); Sed new_sed = Sed(argv, &input);
std::ofstream output = output(new_sed.getFilename()); std::ofstream output(new_sed.getFilename());
while (new_sed.line(output)){} if (!output.is_open()) {
std::cerr << "Error: Could not open the file for writing." << std::endl;
return 1;
}
while (new_sed.line(output))
{
}
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 15:58:02 by rparodi #+# #+# */ /* Created: 2024/11/05 15:58:02 by rparodi #+# #+# */
/* Updated: 2024/11/05 21:58:30 by rparodi ### ########.fr */ /* Updated: 2024/11/06 16:14:03 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,13 +33,25 @@ const char *Sed::getFilename()
return (_filename.c_str()); return (_filename.c_str());
} }
bool Sed::line(std::ofstream output) bool Sed::line(std::ofstream& output)
{ {
std::string to_ret; std::string to_ret;
if (!std::getline((*_input), to_ret)) if (!std::getline(*_input, to_ret)) {
return (false); return false;
output << to_ret << std::endl; }
std::cout << to_ret << std::endl;
return (true); std::string result;
std::size_t pos = 0;
std::size_t found = 0;
while ((found = to_ret.find(_to_replace, pos)) != std::string::npos) {
result += to_ret.substr(pos, found - pos) + _by_word;
pos = found + _to_replace.length();
}
result += to_ret.substr(pos);
output << result << std::endl;
return true;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 15:27:45 by rparodi #+# #+# */ /* Created: 2024/11/05 15:27:45 by rparodi #+# #+# */
/* Updated: 2024/11/05 21:58:59 by rparodi ### ########.fr */ /* Updated: 2024/11/06 15:56:45 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,7 @@ class Sed
public: public:
Sed(char **argv, std::ifstream *input); Sed(char **argv, std::ifstream *input);
~Sed(); ~Sed();
bool line(std::ofstream output); bool line(std::ofstream& output);
const char *getFilename(); const char *getFilename();
private: private:
std::string _filename; std::string _filename;