diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/piscine/io_replace_line/replace_line.c | |
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/io_replace_line/replace_line.c')
| -rw-r--r-- | graphs/piscine/io_replace_line/replace_line.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/graphs/piscine/io_replace_line/replace_line.c b/graphs/piscine/io_replace_line/replace_line.c new file mode 100644 index 0000000..7fd0e2a --- /dev/null +++ b/graphs/piscine/io_replace_line/replace_line.c @@ -0,0 +1,50 @@ +#define _POSIX_C_SOURCE 200809L + +#include <stdio.h> +#include <stdlib.h> + +int replace_line(const char *file_in, const char *file_out, const char *content, + int n) +{ + FILE *a = fopen(file_out, "w"); + if (a == NULL) + { + return -1; + } + FILE *r = fopen(file_in, "r"); + if (r == NULL) + { + return -1; + } + + char *buf = NULL; + ssize_t e; + int l = 0; + size_t count = 0; + while ((e = getline(&buf, &count, r)) != 0 && e != -1) + { + if (l == n) + { + if (fputs(content, a) == EOF) + { + free(buf); + return -1; + } + } + else + { + if (fputs(buf, a) == EOF) + { + free(buf); + return -1; + } + } + l++; + } + + fclose(a); + fclose(r); + free(buf); + + return 0; +} |
