Skip to content

Commit 9c36f0e

Browse files
committed
Speak gently, she can hear
1 parent fbd3d67 commit 9c36f0e

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

0x15-file_io/2-append_text_to_file.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "main.h"
2+
3+
/**
4+
* append_text_to_file - append text to the end of a file
5+
* @filename: the name of the file
6+
* @text_content: NULL terminated string to the end of the file
7+
* Return: 1 or -1
8+
*/
9+
int append_text_to_file(const char *filename, char *text_content)
10+
{
11+
int fd, a, ntext;
12+
13+
if (filename == NULL)
14+
return (-1);
15+
16+
fd = open(filename, O_WRONLY | O_APPEND);
17+
if (fd < 0)
18+
return (-1);
19+
20+
if (text_content != NULL)
21+
{
22+
for (ntext = 0; text_content[ntext]; ntext++)
23+
;
24+
a = write(fd, text_content, ntext);
25+
26+
if (a < 0)
27+
return (-1);
28+
}
29+
30+
close(fd);
31+
32+
return (1);
33+
}

0x15-file_io/2-main.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "main.h"
4+
5+
/**
6+
* main - check the code
7+
*
8+
* Return: Always 0.
9+
*/
10+
int main(int ac, char **av)
11+
{
12+
int res;
13+
14+
if (ac != 3)
15+
{
16+
dprintf(2, "Usage: %s filename text\n", av[0]);
17+
exit(1);
18+
}
19+
res = append_text_to_file(av[1], av[2]);
20+
printf("-> %i)\n", res);
21+
return (0);
22+
}

0x15-file_io/c

16.6 KB
Binary file not shown.

0x15-file_io/hello

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

0x15-file_io/main.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ssize_t read_textfile(const char *filename, size_t letters);
1313
int _putchar(char c);
1414
int create_file(const char *filename, char *text_content);
15+
int append_text_to_file(const char *filename, char *text_content);
1516

1617

1718

0 commit comments

Comments
 (0)