File tree 5 files changed +57
-0
lines changed
5 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ Hello World!
Original file line number Diff line number Diff line change 12
12
ssize_t read_textfile (const char * filename , size_t letters );
13
13
int _putchar (char c );
14
14
int create_file (const char * filename , char * text_content );
15
+ int append_text_to_file (const char * filename , char * text_content );
15
16
16
17
17
18
You can’t perform that action at this time.
0 commit comments