diff options
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; +} |
