blob: 7fd0e2a98f60acfcbfae205b6be0f411643be9a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}
|